Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

modifying form field values - plz help

Status
Not open for further replies.

siuk

Programmer
Aug 23, 2001
38
0
0
GB
Hi,

Im trying to make a script which will increment a text form field value by 1 each time a button is pressed.

Here is my code so far:
=======================
<SCRIPT language=&quot;JavaScript&quot;>
function function1(afield){
var var1 = afield;
var val1 = eval(&quot;form1.&quot;+var1+&quot;.value&quot;)

var result = val1 + 1;

eval(&quot;form1.&quot;+var1+&quot;.value&quot;) = result;

}
</SCRIPT>

<form name=form1>
<p> </p>
<p> </p>
<p> </p>
<input type=text name=field1 value=-2 size=&quot;4&quot;><input type=submit value=Work onclick=&quot;function1('field1');&quot;>
</form>
==================================

This script has errors... does anyone know how i can modify the field value without causing the error 'cannot assign to a function result'

Any help would be greatly appreciated!

Thanks
 
This is a simple fix for it :

<HTML>
<HEAD>
</HEAD>
<BODY>

<SCRIPT>
function populate(afield)
{
afield.value = parseInt(afield.value) + 1
document.form1.submit();
}
</SCRIPT>

<form name=form1>
<input type=text name=field1 value=-2 size=&quot;4&quot;>
<input type=button value=Work onclick=&quot;populate(document.form1.field1);&quot;>
</form>

</BODY>
</HTML>

You have to use parseInt() to make sure the value is treated as a number otherwise it would be treated as text.

I hope this helps. Gary Haran
 
<html>
<head>
<SCRIPT language=&quot;JavaScript&quot;>
v=-2;
function f1(){
v+=1;
document.form1.field1.value=v;
}
</SCRIPT>
<form name=&quot;form1&quot;>
<p> </p>
<p> </p>
<p> </p>
<input type=&quot;text&quot; name=&quot;field1&quot; size=4 value=-2>
<input type=&quot;button&quot; value=&quot;Work&quot; onclick=&quot;f1();&quot;>
</form>
</body>
</html>
 
Thanks Very Much.. ParseInt is very cool! : )
 
Hi siuk, xutopia,

xutopia, what is the meaning of document.form1.submit(); ? I don't understand this.
With the document.form1.submit(); line it restores the value -2 each time.

Without this line it works fine.

Erik
<!-- My sport: Boomerang throwing !!
This year I will participate at the World Championships in Germany. (!! Many Happy Returns !! -->
 
the reason Boomerand is that the default value for the field is -2. This means that when the page loads the value you will see is -2. So when you submit the page it refreshes and the default value is once again set to minus two.

<input type=text name=field1 value=-2 size=&quot;4&quot;>

I hope this helps understand.
Gary Haran
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top