linux1880 Posted June 15, 2010 Share Posted June 15, 2010 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 https://forums.phpfreaks.com/topic/204838-please-help-me-to-complete-this-javascript/ Share on other sites More sharing options...
haku Posted June 15, 2010 Share Posted June 15, 2010 I can't understand your question. Link to comment https://forums.phpfreaks.com/topic/204838-please-help-me-to-complete-this-javascript/#findComment-1072390 Share on other sites More sharing options...
RIRedinPA Posted June 15, 2010 Share Posted June 15, 2010 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 https://forums.phpfreaks.com/topic/204838-please-help-me-to-complete-this-javascript/#findComment-1072419 Share on other sites More sharing options...
linux1880 Posted June 16, 2010 Author Share Posted June 16, 2010 Thanks RIRedinPA If i want to show the result as document.write how do i put the result in another div ? Link to comment https://forums.phpfreaks.com/topic/204838-please-help-me-to-complete-this-javascript/#findComment-1072717 Share on other sites More sharing options...
haku Posted June 16, 2010 Share Posted June 16, 2010 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 https://forums.phpfreaks.com/topic/204838-please-help-me-to-complete-this-javascript/#findComment-1072753 Share on other sites More sharing options...
linux1880 Posted June 16, 2010 Author Share Posted June 16, 2010 Perthaps, because it's my poor knowledge in javascript, none of the above example works for me Link to comment https://forums.phpfreaks.com/topic/204838-please-help-me-to-complete-this-javascript/#findComment-1072917 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.