vendredi 20 février 2015

3 text box Math in Javascript


Hi I am NewBee in Javascript. This is my second week.


Below is the code that has a form with three input fields.

The relationship of the fields is:



  • the second field is twice the value of the first field

  • the third field is the square of the first field


I have managed to do the above but i am not able to do the below :

If a user enters a value in the second or third field, the script should calculate the appropriate value in the other fields.

Currently the code works well ONLY if I enter the value in the first field.


I hope I explained well in other words : how do I enter say 144 in the last textbox and the other 2 textboxes show 12 and 24 respectively. Or If I enter 24 first and first and the third text boxes show 12 and 144.


Thanks Vipul



<html>
<head>
<script>
window.onload = init;

function init() {
var button = document.getElementById("usrButton");
button.onclick = save;
onkeyup = doMath;

function doMath(){
var base = document.getElementById("base").value;
var baseNumber_timesTwo = document.getElementById("baseNumber_timesTwo").value = (base*2);
var baseNumber_square = document.getElementById("baseNumber_square").value = (base*base) ;
}
}
</script>
</head>
<body>
<form>
<input type="text" name="base" id="base" onkeyup= "doMath()">
<br><br>
<input type="text" name="baseNumber_timesTwo" id="baseNumber_timesTwo" onkeyup= doMath()>
<br><br>
<input type="text" name="baseNumber_square" id="baseNumber_square" onkeyup= doMath()> <br><br>
</form>
</body>
</html>




Aucun commentaire:

Enregistrer un commentaire