crazysaint Posted June 11, 2007 Share Posted June 11, 2007 i have the following page and i would like to modify it using html such that in the sum text box it gives me the sum of x and y <html> <body> <form name="frm1"> x<input type="text" name="txt1" value="5"><br> y<input type="text" name="txt2" value="10"><br> sum<input type="text" name="sum"> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/55097-html-addition/ Share on other sites More sharing options...
ToonMariner Posted June 11, 2007 Share Posted June 11, 2007 do you wantthis to happen the instant they chenge numbers? you have a submit buttone which would pass the info to a page for processing but I suspect you want this to do it insitu... <html> <head> <script type="text/javascript"> function calc() { var a = document.getElementById('txt1').value; var b = document.getElementById('txt2').value; document.getElementById('answer').value = a + b; } </script> </head> <body> <form name="frm1"> x<input type="text" name="txt1" id="txt1" value="5" onkeyup="calc();"><br> y<input type="text" name="txt2" id="txt2" value="10" onkeyup="calc();"><br> <input type="text" name="answer" id="answer" value="" /> sum<input type="text" name="sum"> </form> </body> </html> you can swap onkeyup for onchange or what ever trigger you prefer using.... Quote Link to comment https://forums.phpfreaks.com/topic/55097-html-addition/#findComment-272439 Share on other sites More sharing options...
tarun Posted June 11, 2007 Share Posted June 11, 2007 Just Some Simple JS Will Do The Trick <html> <body> <script> function add() { var x = document.getElementById("txt1").value; var y = document.getElementById("txt2").value; document.getElementById("sum").value =(x*1)+(y*1); } </script> <form name="frm1"> <input type="text" id="txt1" value="5"> + <input type="text" id="txt2" value="10"><input type="button" value="Add" onclick="add()"> sum<input type="text" id="sum"> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/55097-html-addition/#findComment-272450 Share on other sites More sharing options...
tarun Posted June 11, 2007 Share Posted June 11, 2007 Ooopps Another Reply Was Posted Before I Did Mine And I Didn't Even Realise LOL Sorry Quote Link to comment https://forums.phpfreaks.com/topic/55097-html-addition/#findComment-272451 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.