Dr John Stockton wrote in message news:uF******** ******@merlyn.d emon.co.uk...
Papajo posted: This simple script counts up or down with a button click, now can it be
modified so the count won't go below zero?
Thanks, Joe
<form>
<input type=text name=amount size=4 value=>
<input type=button value="up"
onClick="javasc ript:this.form. amount.value++; ">
<input type=button value="down"
onClick="javasc ript:this.form. amount.value--;">
</form>
Consider and adapt
<input type=button value="5"
onClick="javasc ript:this.value -= this.value>0">
Read the newsgroup FAQ for other matters.
hmmm...
three questions:
1. why the "javascript :" in onClick="javasc ript:this.value -= this.value>0">
2. if I want to go up, I run into "string" problems:
<input type=text name=amount size=4 value=5>
<input type=button value="down"
onClick="this.f orm.amount.valu e -= this.form.amoun t.value>0">
works wonderfully, but
<input type=button value="up"
onClick="this.f orm.amount.valu e += +this.form.amou nt.value<10">
returns 5true (5truefalse on the next...)
<input type=button value="up"
onClick="this.f orm.amount.valu e += +(this.form.amo unt.value<10)">
returns 51 (and 510 on the second click)
I thought unary+ would type-convert to number, but I notice:
stringVar += +stingVar --> stringVar += numVar
so I end up with
<input type=button value="up"
onClick="this.f orm.amount.valu e = +this.form.amou nt.value + +(this.form.amo unt.value<10)">
isn't there a shortcut or is type-conversion only really good for - * and /?
ok, came up with the unpretty:
<input type=button value="up"
onClick="this.f orm.amount.valu e -= +(this.form.amo unt.value<10) * -1">
this way, the -= type-converts both sides additionally I need to invert the number (- * - = +)