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))" /> Quote Link to comment Share on other sites More sharing options...
haku Posted June 15, 2010 Share Posted June 15, 2010 I can't understand your question. Quote Link to comment 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 Quote Link to comment 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 ? Quote Link to comment 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> Quote Link to comment 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.