I am trying to create form which automatically alters a text box when the user uses keystrokes .
This fiddle illustrates what I am attempting however I have had no success.
Essentially I want the 3rd text box to show the "Number of People" multiplied by the "Price", how can I do this?
HTML
<form action="postenquiry.php" method="post" name="myform">
<label>Num of People</label>
<input type="text" name="qty" />
<br/>
<label>Price</label>
<input type="text" name="Cost" onkeyup="calculate()" />
<br/>
<input type="text" name="textbox5" />
</form>
Javascript
function calculate() {
if (isNaN(document.forms["myform"]["qty"].value) || document.forms["myform"]["qty"].value == "") {
var text1 = 0;
} else {
var text1 = parseInt(document.forms["myform"]["qty"].value);
}
if (isNaN(document.forms["myform"]["Cost"].value) || document.forms["myform"]["Cost"].value == "") {
var text2 = 0;
} else {
var text2 = parseFloat(document.forms["myform"]["Cost"].value);
}
document.forms["myform"]["textbox5"].value = (text1 * text2);
}
onkeyup="calculate()"he attached a function in global escope, but when the function is declared inside theonloadevent, it is not global. In theheadtag, outside any block it is global, therefore it can be called by the event.