Jump to content

html addition


crazysaint

Recommended Posts

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>

Link to comment
https://forums.phpfreaks.com/topic/55097-html-addition/
Share on other sites

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....

Link to comment
https://forums.phpfreaks.com/topic/55097-html-addition/#findComment-272439
Share on other sites

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>

Link to comment
https://forums.phpfreaks.com/topic/55097-html-addition/#findComment-272450
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.