-
Flash Developer on a break
Javascript - Number addition in textfield
This is the code:
PHP Code:
<html>
<head>
<script>
function stat_min(thevalue) {
eval("document."+thevalue+".value") --;
}
function stat_plus(thevalue) {
eval("document."+thevalue+".value") ++;
}
</script>
</head>
<body >
<INPUT class='input' TYPE='TEXT' NAME='str' SIZE=5 MAXLENGTH=5
VALUE=2><input class="input" type="button" value=" + "
onclick="stat_plus('str')">
</body>
</html>
It doesn't seem to work.. It's probably a simple solution. Can anyone help me?
Already thanks,
Jeroen
| "Keep Flashing!"-Jeroen
PHP multiplayer? Check this thread: PHPgateway
-
Try this:
PHP Code:
function incrementTxtField(nameOfTxtField)
{
var value;
value = parseInt(document[nameOfTxtField].value);
if(value == 'NaN')
{
document[nameOfTxtField].value = 0;
} else {
document[nameOfTxtField].value = ++value;
}
}
parseInt attempts to convert a string to number and returns 'NaN' if it fails. The value of the text field will always be a string so it is necessary to do this.
I'm sure you can work out how to make the decrement function!
-
Flash Developer on a break
Your code didn't seam to work..
But I changed my own into this after some trying.. =P
PHP Code:
<script language="JavaScript"><!--
function stat_min(f,stat) {
eval("f."+stat+".value--");
}
function stat_plus(f,stat) {
eval("f."+stat+".value++");
}
// --></script>
Kinda forgot to put the eval() around everything.
Btw - do you know a way of making a textfield NOT editable by the user, only by javascript?
Tnx
| "Keep Flashing!"-Jeroen
PHP multiplayer? Check this thread: PHPgateway
-
Flash Developer on a break
Nevermind.. I saved the number in the textfield to a JS var and made a function that is called onChange of the txt-field.
| "Keep Flashing!"-Jeroen
PHP multiplayer? Check this thread: PHPgateway
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|