Jump to content

Multiplying two textbox values

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
njr1489

njr1489

    Learning Programmer

  • Members
  • PipPipPip
  • 70 posts
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.


#2
Brandon W

Brandon W

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,828 posts
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.
jQuery Selectors Tutorial - jQuery Striped Table tutorial - jQuery Events - jQuery Validation

Sorry if I don't post as often as I did, I'll try to get here as much as possible! I'm working my bum off to get this scholarship and other stuff!


#3
CameronMauch

CameronMauch

    Newbie

  • Members
  • Pip
  • 3 posts

njr1489 said:

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