Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Multiplying two textbox values


  • Please log in to reply
2 replies to this topic

#1 njr1489

njr1489

    CC Resident

  • Just Joined
  • PipPipPipPip
  • 69 posts

Posted 24 February 2009 - 06:35 PM

I am trying to calculate two values that a user would input in two text boxes. With my current code throws an exception because of the the assignment of total. I'm not really sure how else I could go about doing this... This is the current code I have.

<html>
<head>
<script>
var total = document.frm.len.value * document.frm.wid.value;

function calculate()
{
     alert(total);
}
</script>
</head>
<body>

<form name="frm">
<table>
<tr>
<td>Length</td>
<td><input type="text" name="len" /></td>
</tr>

<tr>
<td>Width</td>
<td><input type="text" name="wid" /></td>
</tr>

<tr>
<td><input type="button" name="Submit" value="Submit" onClick="calculate()" /></td>
</tr>
</table>
</form>

</body>
</html>

---------------------------------------------------------------------------

EDIT:

I got it, here's the results for those of you that want it:

<html>
<head>
<script>

function calculate(length, width)
{
     alert(length.value * width.value);
}
</script>
</head>
<body>

<form name="frm">
<table>
<tr>
<td>Length</td>
<td><input type="text" name="len" /></td>
</tr>

<tr>
<td>Width</td>
<td><input type="text" name="wid" /></td>
</tr>

<tr>
<td><input type="button" name="Submit" value="Submit" onClick="calculate(document.frm.len, document.frm.wid)" 

/></td>
</tr>
</table>
</form>

</body>
</html>

Edited by njr1489, 24 February 2009 - 07:35 PM.
Figured it out myself.

  • 0

#2 Brandon W

Brandon W

    CC Mentor

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2092 posts
  • Location:Ipswich, Australia
  • Programming Language:C, Java
  • Learning:Java, C++, JavaScript

Posted 24 February 2009 - 11:33 PM

I was just about to say something along those lines. Good work mate :)

Thanks for posting the final code too mate, this will help people with the same problem.
  • 0
I've returned...

#3 CameronMauch

CameronMauch

    CC Lurker

  • Just Joined
  • Pip
  • 3 posts

Posted 31 March 2009 - 06:05 AM

I am trying to calculate two values that a user would input in two text boxes. With my current code throws an exception because of the the assignment of total. I'm not really sure how else I could go about doing this... This is the current code I have.

<html>
<head>
<script>
var total = document.frm.len.value * document.frm.wid.value;

function calculate()
{
     alert(total);
}
</script>
</head>
<body>

<form name="frm">
<table>
<tr>
<td>Length</td>
<td><input type="text" name="len" /></td>
</tr>

<tr>
<td>Width</td>
<td><input type="text" name="wid" /></td>
</tr>

<tr>
<td><input type="button" name="Submit" value="Submit" onClick="calculate()" /></td>
</tr>
</table>
</form>

</body>
</html>

---------------------------------------------------------------------------

EDIT:

I got it, here's the results for those of you that want it:

<html>
<head>
<script>

function calculate(length, width)
{
     alert(length.value * width.value);
}
</script>
</head>
<body>

<form name="frm">
<table>
<tr>
<td>Length</td>
<td><input type="text" name="len" /></td>
</tr>

<tr>
<td>Width</td>
<td><input type="text" name="wid" /></td>
</tr>

<tr>
<td><input type="button" name="Submit" value="Submit" onClick="calculate(document.frm.len, document.frm.wid)" 

/></td>
</tr>
</table>
</form>

</body>
</html>


The reason you saw an exception was probably because the javascript began executing before the page had finished loading. The two input elements may not have existed yet. Use <body onload="..."> or the superior jQuery $(document).ready().

Cameron
  • 0




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download