469,307 Members | 1,804 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,307 developers. It's quick & easy.

Adding 1 to form field with button

Hi,

I´m building a form and decided to throw myself at some Javascript, even though im definetely no expert. Thats why i now need help :-/

I´m building a fairly simple HTML form where the user types in data about himself.

One of the form fields contain the number 0 which can then be changed by the user manually (which then results in some calculations etc...). However i would very much like to have a button that is able to add 1 to the form field when it is clicked - also it would be nice to have one that subtracts 1.

I am guessing i need a piece of code that adds +1 (or subtracts) from the value of the form field? But how i would do that i have no idea. I hope someone here can help me out?

Thx for the help and a great place to learn Javascript :-)
Mar 26 '08 #1
1 1647
Here is an example that will add or subtract one:

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3.  
  4. <script language="javascript" type="text/javascript">
  5.  var current;
  6.  function add_subtract(numb)
  7.  {
  8.    current = parseInt(document.getElementById("number").value);    // gets value from text  (parseInt gets first occurrence of a number from text)
  9.    current+=numb;    // adds variable numb to current
  10.    document.getElementById("number").value = current;    // puts new value into text
  11.  }
  12. </script>
  13.  
  14. </head>
  15. <body>
  16. <input type="text" id="number" value="0">
  17. <input type="button" value="Add One" onclick="add_subtract(1)">
  18. <input type="button" value="Subtract One" onclick="add_subtract(-1)">
  19.  
  20. </body>
  21. </html>
  22.  
I hope I helped.
Mar 26 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by Amir Hardon | last post: by
3 posts views Thread by Jim Heavey | last post: by
5 posts views Thread by johnny | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.