View Full Version : Adding in a text box

02-08-2007, 05:35 PM
I struggling to create/find some code that will do the following

A text starts with the number "0" in it and when the user either clicks a button at the side of the box or click in the box (only one way is required) the number changes to 1 if pressed again it changes to 2 and so one. If any one could help with this it would be great

Thank you

02-08-2007, 05:47 PM
onclick="this.form.yourtextboxname.value = parseInt(this.form.yourtextboxname.value)+1;"

Add a check for isNaN if you don't control the value.

02-08-2007, 06:00 PM
Where do I add this- I have created a form and put a text box in it with the name text

02-08-2007, 06:15 PM
<input type="text" name="t1"><br>
<input type="button" onclick="this.form.t1.value = parseInt(this.form.t1.value)+1;">

02-08-2007, 06:21 PM
This kinda works, when 1 first press the button it says NaN if I delete this and put the number 1 in it then press the button it adds 1 (every time I click)

02-09-2007, 03:30 PM
Like I said, you have to add the check for isNaN

A simple google search will show you how to do this.

02-09-2007, 05:07 PM
Checks for NaN

By using the OR statment '||'

( is a number || instead )

<input type="text" name="t1"><br>
<input type="button" value="+1" onclick="this.form.t1.value = (parseInt(this.form.t1.value)+1 || 1);">