By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,605 Members | 2,129 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,605 IT Pros & Developers. It's quick & easy.

Counter Script

P: n/a
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="javascript:this.form.amount.value++;">
<input type=button value="down"
onClick="javascript:this.form.amount.value--;">
</form>

Sep 30 '05 #1
Share this Question
Share on Google+
22 Replies


P: n/a

Papajo wrote:
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="javascript:this.form.amount.value++;">
<input type=button value="down"
onClick="javascript:this.form.amount.value--;">
</form>


There is no need for the javascript protocol. I prefer not to do
inline javascript unless I can't help it.

Place script in between the <head> tags and you can something like the
following:

<script type = "text/javascript">
function countUp(obj)
{
obj.form.amount.value++;
}

function countDown(obj)
{
amtObj = obj.form.amount;

if(amtObj.value > 1)
{
obj.form.amount.value--;
}
}
</script>

As for your form:

<form>
<input type = "text" name = "amount">
<input type = "button" value = "up" onClick = "countUp(this);">
<input type = "button" value = "down" onClick = "countDown(this);">
</form>

Sep 30 '05 #2

P: n/a
Lee
Papajo said:

This simple script counts up or down with a button click, now can it be
modified so the count won't go below zero?
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 Thanks, Joe

<form>
<input type=3Dtext name=3Damount size=3D4 value=3D>
<input type=3Dbutton value=3D"up"
onClick=3D"javascript:this.form.amount.value++; ">
<input type=3Dbutton value=3D"down"
onClick=3D"javascript:this.form.amount.value--;">
</form>


<form>
<input type="text" name="amount" size="4" value="0">
<input type="button" value="up"
onClick="this.form.amount.value++;">
<input type="button" value="down"
onClick="if(this.form.amount.value>'0')this.form.a mount.value--;">
</form>

Sep 30 '05 #3

P: n/a
Lee wrote on 30 sep 2005 in comp.lang.javascript:
Papajo said:

<form>
<input type="text" name="amount" size="4" value="0">
<input type="button" value="up"
onClick="this.form.amount.value++;">
<input type="button" value="down"
onClick="if(this.form.amount.value>'0')this.form.a mount.value--;">
</form>


this.form.amount.value>'0'
the parentheses are unnecessary.
Much better to disable the down button when 0 is reached:

========================
<form>

<input type="text" name="amount" size="4" value="2">

<input type="button" value="up"
onClick="this.form.amount.value++;
document.getElementById('down').disabled=false">

<input type="button" value="down" id='down'
onClick="this.disabled=(--this.form.amount.value==0);">

</form>
=========================

ie6 tested

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Sep 30 '05 #4

P: n/a
Lee
Evertjan. said:

Lee wrote on 30 sep 2005 in comp.lang.javascript:
Papajo said:

<form>
<input type="text" name="amount" size="4" value="0">
<input type="button" value="up"
onClick="this.form.amount.value++;">
<input type="button" value="down"
onClick="if(this.form.amount.value>'0')this.form.a mount.value--;">
</form>


this.form.amount.value>'0'
the parentheses are unnecessary.
Much better to disable the down button when 0 is reached:

========================
<form>

<input type="text" name="amount" size="4" value="2">

<input type="button" value="up"
onClick="this.form.amount.value++;
document.getElementById('down').disabled=false">

<input type="button" value="down" id='down'
onClick="this.disabled=(--this.form.amount.value==0);">

</form>


How is that much better? It's insignificantly more efficient
in execution, and much more complicated to implement (and for
a new user to understand).

Sep 30 '05 #5

P: n/a
JRS: In article <28****************@storefull-3337.bay.webtv.net>,
dated Fri, 30 Sep 2005 14:04:09, seen in news:comp.lang.javascript,
Papajo <do*****@webtv.net> 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="javascript:this.form.amount.value++;">
<input type=button value="down"
onClick="javascript:this.form.amount.value--;">
</form>


Consider and adapt

<input type=button value="5"
onClick="javascript:this.value -= this.value>0">

Read the newsgroup FAQ for other matters.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Sep 30 '05 #6

P: n/a
Thank you for the responses, I went with Lee's script, it was the
easiest and best for my application. Joe

<form>
<input type="text" name="amount" size="4" value="">

<input type="button" value="up"
onClick="javascript:this.form.amount.value++;">
<input type="button" value="down"
onClick="if(javascript:this.form.amount.value>'0') this.form.amount.value--;">
</form>

Oct 1 '05 #7

P: n/a
Dr John Stockton wrote in message news:uF**************@merlyn.demon.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="javascript:this.form.amount.value++;">
<input type=button value="down"
onClick="javascript:this.form.amount.value--;">
</form>


Consider and adapt

<input type=button value="5"
onClick="javascript:this.value -= this.value>0">

Read the newsgroup FAQ for other matters.


hmmm...
three questions:
1. why the "javascript:" in onClick="javascript: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.form.amount.value -= this.form.amount.value>0">

works wonderfully, but

<input type=button value="up"
onClick="this.form.amount.value += +this.form.amount.value<10">
returns 5true (5truefalse on the next...)
<input type=button value="up"
onClick="this.form.amount.value += +(this.form.amount.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.form.amount.value = +this.form.amount.value + +(this.form.amount.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.form.amount.value -= +(this.form.amount.value<10) * -1">

this way, the -= type-converts both sides additionally I need to invert the number (- * - = +)
Oct 1 '05 #8

P: n/a
Lee wrote on 01 okt 2005 in comp.lang.javascript:
Evertjan. said:

Lee wrote on 30 sep 2005 in comp.lang.javascript:
Papajo said:

<form>
<input type="text" name="amount" size="4" value="0">
<input type="button" value="up"
onClick="this.form.amount.value++;">
<input type="button" value="down"
onClick="if(this.form.amount.value>'0')this.form.a mount.value-
-;">
</form>


this.form.amount.value>'0'
the parentheses are unnecessary.
Much better to disable the down button when 0 is reached:

========================
<form>

<input type="text" name="amount" size="4" value="2">

<input type="button" value="up"
onClick="this.form.amount.value++;
document.getElementById('down').disabled=false">

<input type="button" value="down" id='down'
onClick="this.disabled=(--this.form.amount.value==0);">

</form>


How is that much better? It's insignificantly more efficient
in execution, and much more complicated to implement (and for
a new user to understand).


The much better is the computer/human interface showing that the down-
button does not function when the value is zero.

A down button that looks and feels functinal but does not count down is a
bad concept, when disabling is possible.

Proramming is not only about functioning but also about functionality.

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Oct 1 '05 #9

P: n/a
To answer your question about the "javascript:" in the onClick is that
this script is being used on a simple webtv browser, it wouldn't work
without it. Thanks again Joe
PS: sorry I didn't mention it in my original post

Oct 1 '05 #10

P: n/a
Papajo wrote:

"To answer your question about the "javascript:" in the onClick is that
this script is being used on a simple webtv browser, it wouldn't work
without it. Thanks again Joe PS: sorry I didn't mention it in my
original post"

Wrong answer.

In over 5 1/2 years I've never known that protocol to be required in an
onclick with WebTV. I've never seen it used in an onclick. As a matter
of fact take the code from your original post to your test bed, remove
the javascript:, and it works.

While you're there go ahead and make the necessary corrections to your
invalid markup.

Later, Art.

Oct 1 '05 #11

P: n/a
Lee
Evertjan. said:
The much better is the computer/human interface showing that the down-
button does not function when the value is zero.

A down button that looks and feels functinal but does not count down is a
bad concept, when disabling is possible.


There are two (main) schools of thought on interface design.
Some, like yourself, believe that a control that is not going
to do what the user expects should be disabled. I hate that.
I prefer to be able to push any button, and either be told
why it's not going to function, or simply have it do nothing,
if it's obvious why nothing is happening (as in this case).

A few months ago I found myself supporting an interface that
adhered to your school. Most of my support calls were asking
why some button or another was disabled. Lots of frustration.

Oct 1 '05 #12

P: n/a
Well this is the first piece of good advice you've given me in 5
years, usually it's worthless, keep up the good work, your getting
better

Oct 2 '05 #13

P: n/a
Lee wrote on 02 okt 2005 in comp.lang.javascript:
Evertjan. said:
The much better is the computer/human interface showing that the down-
button does not function when the value is zero.

A down button that looks and feels functinal but does not count down
is a bad concept, when disabling is possible.


There are two (main) schools of thought on interface design.
Some, like yourself, believe that a control that is not going
to do what the user expects should be disabled. I hate that.
I prefer to be able to push any button, and either be told
why it's not going to function, or simply have it do nothing,
if it's obvious why nothing is happening (as in this case).

A few months ago I found myself supporting an interface that
adhered to your school. Most of my support calls were asking
why some button or another was disabled. Lots of frustration.


If you want to hide behind a 'school', I don't mind,
but my stated meaning is my own.

The frustration by an not working button is far greater
than that by a disabled button.

However if you wish,
you could ad an explanation to the disabled ones:

<span
onmouseover="buttonwarn(this,'')"
onmouseout="buttonwarn(this,'out')">
<button disabled style='width:211px;'>
Enter
</button>
</span>

<script type='text/javascript'>

var buttonwarnSave;

function buttonwarn(x,y){
x = x.firstChild;
if (!x.disabled) return;
if (y=='out')
x.innerHTML = buttonwarnSave;
else {
buttonwarnSave = x.innerHTML
x.innerHTML = "I am disabled, because ..."
}
}

</script>

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Oct 2 '05 #14

P: n/a
Lee
Evertjan. said:
If you want to hide behind a 'school', I don't mind,
but my stated meaning is my own.
Don't try so hard to be an asshole. I'm not hiding
behind anything. I'm telling you my views and also
mentioned that there are many people who agree with
each of us.
The frustration by an not working button is far greater
than that by a disabled button.


You seem to misunderstand. A disabled button typically
offers no explanation for why it is disabled. That's
frustrating to users who want to push it. Their first
impression is typically that the interface is broken.

A button that can be pushed can pop up an error message,
or, in cases where it is obviously the expected behavior,
have no effect, without causing frustration.

Adding additional text to the button is a ridiculous
solution. You change the page layout and add greatly
to the complexity of the solution.

Oct 2 '05 #15

P: n/a
Lee wrote on 02 okt 2005 in comp.lang.javascript:
Evertjan. said:
If you want to hide behind a 'school', I don't mind,
but my stated meaning is my own.


Don't try so hard to be an asshole. I'm not hiding
behind anything. I'm telling you my views and also
mentioned that there are many people who agree with
each of us.
The frustration by an not working button is far greater
than that by a disabled button.


You seem to misunderstand. A disabled button typically
offers no explanation for why it is disabled. That's
frustrating to users who want to push it. Their first
impression is typically that the interface is broken.

A button that can be pushed can pop up an error message,
or, in cases where it is obviously the expected behavior,
have no effect, without causing frustration.

Adding additional text to the button is a ridiculous
solution. You change the page layout and add greatly
to the complexity of the solution.


Why are you impolite, Lee?

Why do you say "Asshole"?
Did I tell you you idea's were "ridiculous"?

Please stick to arguments if you want to prove your case.

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Oct 2 '05 #16

P: n/a
JRS: In article <Te********************@trueband.net>, dated Fri, 30
Sep 2005 22:50:24, seen in news:comp.lang.javascript, Robi
<me@privacy.net> posted :
Dr John Stockton wrote in message news:uF**************@merlyn.demon.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="javascript:this.form.amount.value++;">
<input type=button value="down"
onClick="javascript:this.form.amount.value--;">
</form>

Consider and adapt

<input type=button value="5"
onClick="javascript:this.value -= this.value>0">

Read the newsgroup FAQ for other matters.


hmmm...
three questions:
1. why the "javascript:" in onClick="javascript:this.value -= this.value>0">


As it was for considering, I preferred not to make more changes; FAQ
4.24 covers javascript:.
2. if I want to go up, I run into "string" problems:
If he had wanted to go up, I would have answered differently.
<input type=text name=amount size=4 value=5>
<input type=button value="down"
onClick="this.form.amount.value -= this.form.amount.value>0">

works wonderfully, but

<input type=button value="up"
onClick="this.form.amount.value += +this.form.amount.value<10">
returns 5true (5truefalse on the next...)
<input type=button value="up"
onClick="this.form.amount.value += +(this.form.amount.value<10)">
returns 51 (and 510 on the second click)

Use something like

<input type=button value="5"
onClick="javascript:this.value -= -(this.value<10)">
I thought unary+ would type-convert to number, but I notice:
stringVar += +stingVar --> stringVar += numVar


It does, but I expect += will concatenate if either side is a string.
--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Oct 2 '05 #17

P: n/a
Lee
Evertjan. said:
If you want to hide behind a 'school', I don't mind,


Perhaps it's a cultural difference, but people who say
things like that are considered "assholes" by the people
I know. It's a few degrees beyond "impolite".

Oct 3 '05 #18

P: n/a
Lee wrote on 03 okt 2005 in comp.lang.javascript:
Evertjan. said:

If you want to hide behind a 'school', I don't mind,


Perhaps it's a cultural difference, but people who say
things like that are considered "assholes" by the people
I know. It's a few degrees beyond "impolite".


So the people you know, would not, if I had said that I did mind?

Or is what I said not important to be assigned to that cathegory?

I suggest you wouldn't hide behind "people you know".
Why should they be right?

Perhaps you know strange people, Lee, living in strange cultures.

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Oct 3 '05 #19

P: n/a
Lee
Evertjan. said:

Lee wrote on 03 okt 2005 in comp.lang.javascript:
Evertjan. said:

>If you want to hide behind a 'school', I don't mind,
Perhaps it's a cultural difference, but people who say
things like that are considered "assholes" by the people
I know. It's a few degrees beyond "impolite".


So the people you know, would not, if I had said that I did mind?


I'm sorry this seems to difficult for you to understand,
but it's the accusation of "hiding behind" other who share
my opinions that is offensive. You might recall that in the
original message, I also mentioned that others share your
opinion.

I suggest you wouldn't hide behind "people you know".
Why should they be right?


Do you suppose that might be why I suggested that our disagreement
might be due to cultural differences? A culture in which it is
not considered impolite to accuse people of "hiding behind" others
who share their opinions seems very strange to me.

Oct 3 '05 #20

P: n/a
Lee wrote on 03 okt 2005 in comp.lang.javascript:
Evertjan. said:

Lee wrote on 03 okt 2005 in comp.lang.javascript:
> Evertjan. said:
>
>>If you want to hide behind a 'school', I don't mind,

Perhaps it's a cultural difference, but people who say
things like that are considered "assholes" by the people
I know. It's a few degrees beyond "impolite".


So the people you know, would not, if I had said that I did mind?


I'm sorry this seems to difficult for you to understand,
but it's the accusation of "hiding behind" other who share
my opinions that is offensive. You might recall that in the
original message, I also mentioned that others share your
opinion.


You are welcome to feel sorry, but that is not necessary, I am not
offended by people that have different views over culturals
offensiveness.

This is an international NG, so local feelings can not always be
considered.
I suggest you wouldn't hide behind "people you know".
Why should they be right?


Do you suppose that might be why I suggested that our disagreement
might be due to cultural differences? A culture in which it is
not considered impolite to accuse people of "hiding behind" others
who share their opinions seems very strange to me.


I think that is your personal problem, but reacting an international NG
the way you did is not. By using words that are clearly ment(!) to be
offensive ["asshole"] clearly marks your reaction in a different level.

I am sure I never wanted to offend you. About the opposite, however I am
not so sure.

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Oct 3 '05 #21

P: n/a

Lee wrote:
Papajo said:

This simple script counts up or down with a button click, now can it be
modified so the count won't go below zero?
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 Thanks, Joe

<form>
<input type=3Dtext name=3Damount size=3D4 value=3D>
<input type=3Dbutton value=3D"up"
onClick=3D"javascript:this.form.amount.value++; ">
<input type=3Dbutton value=3D"down"
onClick=3D"javascript:this.form.amount.value--;">
</form>


<form>
<input type="text" name="amount" size="4" value="0">
<input type="button" value="up"
onClick="this.form.amount.value++;">
<input type="button" value="down"
onClick="if(this.form.amount.value>'0')this.form.a mount.value--;">
</form>

Hi all. Came upon this thread by chance. Very informative (if we
ignore the fighting). I have a simple stupid question. Why do you
enclose 0 in quotes? like "if(this.form.amount.value>'0')". Why not
just ">0"? wouldn't it mess things if like, for example, for some
reason, the field was '00' (which is >'0' I think)?

Oct 3 '05 #22

P: n/a
wrote on 03 okt 2005 in comp.lang.javascript:
<form>
<input type="text" name="amount" size="4" value="0">
<input type="button" value="up"
onClick="this.form.amount.value++;">
<input type="button" value="down"
onClick="if(this.form.amount.value>'0')this.form.a mount.value--
;">
</form>

Hi all. Came upon this thread by chance. Very informative (if we
ignore the fighting). I have a simple stupid question. Why do you
enclose 0 in quotes? like "if(this.form.amount.value>'0')". Why not
just ">0"? wouldn't it mess things if like, for example, for some
reason, the field was '00' (which is >'0' I think)?


The quoting has no purpose, since < and > should convert to numbers
anyway.

Comparing the strings '3'>'0' gives true,
because it compares the ascii 51>48

The safest is always converting the DOM value
[which always is a string], to a number:

onClick="if( +this.form.amount.value > 0) .........

==============

testing:

alert('00'>'0') //true
alert(00 > 0) //false !!!

alert('60'>'00') //true
alert(60 > 00) //true
alert(' 60'>'0') //false !!!!!

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Oct 3 '05 #23

This discussion thread is closed

Replies have been disabled for this discussion.