I'm a little lost here. What I have is a basic menu with an item and a price, set up as table within a form. I have a text input box for the user to enter a value — the number of that item wanted.
I want to know how I use the data entered into the input text box after the user clicks the calculate button, so that I can validate it and use it in an equation. If the user wants "2" of the item, I need to bring that value into my function so I can validate it as a numeric value. I'll then use it as an integer to multiply it by the price associated with that item...
Here is a sample of my code:
function calculate(){
var error = false;
if (isNaN(*"what would I put here??"*){
alert("A numeric value is required when ordering steak");
error = true;
}
else{
var numbercrabcakes = parseInt(-------);
}
if (!error)
{
total = numbercrabcakes * 15 * 1.0825;
window.alert("Your order totals: " + total.toFixed(2))
}
}
.........
<form name="myform">
<script type="text/javascript">
/* <![CDATA[ */
document.write("<table border = '1'>")
document.write("<tr><th>Item Picture</th><th>Item Description</th><th>Item Price</th> \
<th>Quanity</tr>");
document.write("<tr><td><img src = 'images/crabcakes.png'></td><td>Cirtus infused crab \
cakes with a sweet and savory sauce</td><td>$" + price[0] + "</td> \
<td><input type='text' id='crabcakes' size='10' value='0' style='text-align:right;' /></td>
</tr>");
/* ]]> */
</script>
<p id="submit_button"><input type="submit" value="Calculate" onclick="calculate()" \
style="width:150px;height:30px;text-align:center;font-weight:bold;" /></p>
</form>
The function section of the code was given as a sample/guideline to be used and edited for our specific page. Do I need to give the input box an id
or name
and use getElementbyId
so I can check it against the isNaN
then use the parseInt
to convert it as the function shows. I did try that, but it didn't seem to work for me.
If you just want to access the value of your textbox with the id "crabcakes" you could do it like so:
function calculate(){
var value = document.getElementById("crabcakes").value;
var error = false;
if (isNaN(value)){
alert("A numeric value is required when ordering steak");
error = true;
}
else{
var numbercrabcakes = parseInt(value);
}
if (!error)
{
total = numbercrabcakes * 15 * 1.0825;
window.alert("Your order totals: " + total.toFixed(2))
}
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments