Jump to content

Please help me to complete this javascript


linux1880

Recommended Posts

Hi guys i would like this form to be able to user input values in the input and javascript add function will add value and display in HTML, how would i achieve it ? Thanks

 

<script type="text/javascript">

function add(x,y){
z = (x+y);
return z;
}
//alert(add(10,20));
//alert('abc');

</script>


first number: <input type="text" value"" />
second number: <input type="text" value"" />
<input type="button" value="add number" onClick="alert(add(120,23))" />

Link to comment
Share on other sites

You need to clean this up somewhat (if I understand what you're looking to do:

 

HTML:

 


first number: <input type="text" value"" id="firstnum" />
second number: <input type="text" value"" id="secondnum" />
<input type="button" name="btnSubmit" onClick="addvalues();" />

 

Javascript:

 

<script language="javascript">

function addvalues() { 

var firstnum = document.getElementById('firstnum').value;
var secondnum = document.getElementById('secondnum').value;
var total = firstnum + secondnum;
alert(total);
}
</script>

 

Something like that, I haven't tested it, though I would wrap the inputs in a form and use name= instead of id= and then get the values by document.formname.elementname.value

Link to comment
Share on other sites

HTML:

first number: <input type="text" value"" id="firstnum" />
second number: <input type="text" value"" id="secondnum" />
<input type="button" name="btnSubmit" onClick="addvalues();" />
<div id="add_values_result"></div>

 

JS:

<script type="text/javascript">

function addvalues() {
document.getElementById("add_values_result").innterHTML = document.getElementById('firstnum').value + document.getElementById('secondnum').value;
}
</script>

 

Link to comment
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.