Counter Script

P

Papajo

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>
 
W

web.dev

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?
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>
 
L

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.amount.value--;">
</form>
 
E

Evertjan.

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.amount.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
 
L

Lee

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


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).
 
D

Dr John Stockton

JRS: In article <[email protected]>,
dated Fri, 30 Sep 2005 14:04:09, seen in
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?
           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.
 
P

Papajo

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>
 
R

Robi

Dr John Stockton wrote in message news:[email protected]...
Papajo posted:

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 (- * - = +)
 
E

Evertjan.

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

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.
 
P

Papajo

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
 
X

X l e c t r i c

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.
 
L

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.
 
P

Papajo

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
 
E

Evertjan.

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


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>
 
L

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.
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Fri, 30
Sep 2005 22:50:24, seen in Robi
Dr John Stockton wrote in message news:[email protected]...

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 said:
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.
 
E

Evertjan.

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


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.


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.
 
L

Lee

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".
 
E

Evertjan.

Lee wrote on 03 okt 2005 in comp.lang.javascript:
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.
 
L

Lee

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

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top