Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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