Forum Moderators: open

Message Too Old, No Replies

input box with number, on click + 1?

         

CodilX

1:09 pm on Oct 1, 2008 (gmt 0)

10+ Year Member



Hi there,

I'm new to JavaScript, and I need some help with this :)

I have an input box:

<input name="inputbox" type="text" value="123" />

What I'm trying to do, is have a "+ 1" a href link, that when pressed, would add +1 to the existing value. So for example if I pressed the +1, the value would increase to 124, if I pressed it again it would increase to 125.

Thanks :)

Trace

1:28 pm on Oct 1, 2008 (gmt 0)

10+ Year Member



Seeing that your input has a name, I'm guessing so does your form. In that case you can affect the value like this;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title> Test </title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
</head>
<body>

<form name="myForm" action="" method="">
<input name="inputbox" type="text" value="123">
</form>

<a href="#" onclick="addOne()">+1</a>

<script type="text/javascript">
addOne = function(){ document.myForm.inputbox.value = document.myForm.inputbox.value*1 + 1 ; }
</script>

</body>
</html>

CodilX

1:37 pm on Oct 1, 2008 (gmt 0)

10+ Year Member



awesome :) thank you so much!

CodilX

1:58 pm on Oct 1, 2008 (gmt 0)

10+ Year Member



oh, and how do I make the javascript to add "0" at the front of the number? so it would show "040", "041" etc?