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))" />

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

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>

 

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.