473,396 Members | 1,858 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Counter Script

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
22 2562

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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Tim Hussein | last post by:
Hi, this is my situation: I have a counter on my MAIN page. It is incremented by requesting a certain "picture" which is the output of a script. I can do this manually with: ...
0
by: deko | last post by:
I use this script to keep count of visits to my site - it's on every page in the site. <?php $file = '/home/post/public_html/viscount'; if ($counter = @file ($file)) { $line = each($counter);...
8
by: Jim in Arizona | last post by:
I've been looking for a counting script to count the number of hits to my HTM and asp web pages on my company's internal website. So far, everything I've seen only works on asp pages. Where would I...
2
by: Carl Gilbert | last post by:
Hi I'm having problems with the following code. All I want to do is keep a counter on the page for every new image recieved without deviating from the "for" and "event". <OBJECT ID="CamImage1"...
7
by: mistral | last post by:
I use htaccess to protect directory and granting access to download file only for the authorized users. Just want implement simple PHP file download counter for single file. I need track the number...
5
by: jasonchan | last post by:
How would you set up a counter in javascript that goes from 5 to 0 This is what i have so far and it is not displaying in the div container "counter" for some reason... <!DOCTYPE html PUBLIC...
3
by: Abhinavnaresh | last post by:
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. ...
9
by: Pygmalion | last post by:
I have found dozen of useful PHP counters on the web. However, nobody is working for my web pages, since administrator does not want to enable the possibility that PHP could be called from HTML. ...
2
by: gumbercules | last post by:
I am designing a site that is regulary updated with new podcasts. I would like to be able to have a counter next to each podcast showing how many hits/listens/plays/views each particular podcast has...
3
by: DavidPr | last post by:
I made a website for a group of people and they want a visitor counter on it. I don't like hit counters because if the site is dead everyone will know it. But, it's their website. I did an...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.