Counter Script

Discussion in 'Javascript' started by Papajo, Sep 30, 2005.

  1. Papajo

    Papajo Guest

    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>
    Papajo, Sep 30, 2005
    #1
    1. Advertising

  2. Papajo

    web.dev Guest

    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>
    web.dev, Sep 30, 2005
    #2
    1. Advertising

  3. Papajo

    Lee Guest

    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>
    Lee, Sep 30, 2005
    #3
  4. Papajo

    Evertjan. Guest

    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

    --
    Evertjan.
    The Netherlands.
    (Replace all crosses with dots in my emailaddress)
    Evertjan., Sep 30, 2005
    #4
  5. Papajo

    Lee Guest

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


    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).
    Lee, Sep 30, 2005
    #5
  6. JRS: In article <>,
    dated Fri, 30 Sep 2005 14:04:09, seen in news:comp.lang.javascript,
    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.

    --
    © 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.
    Dr John Stockton, Sep 30, 2005
    #6
  7. Papajo

    Papajo Guest

    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>
    Papajo, Oct 1, 2005
    #7
  8. Papajo

    Robi Guest

    Dr John Stockton wrote in message news:...
    > 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 (- * - = +)
    Robi, Oct 1, 2005
    #8
  9. Papajo

    Evertjan. Guest

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

    >
    > 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)
    Evertjan., Oct 1, 2005
    #9
  10. Papajo

    Papajo Guest

    Re: Counter Script (Robi)

    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
    Papajo, Oct 1, 2005
    #10
  11. 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.
    X l e c t r i c, Oct 1, 2005
    #11
  12. Papajo

    Lee Guest

    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.
    Lee, Oct 2, 2005
    #12
  13. Papajo

    Papajo Guest

    Re: Counter Script {Art}

    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
    Papajo, Oct 2, 2005
    #13
  14. Papajo

    Evertjan. Guest

    Re: Counter Script [button disabling]

    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)
    Evertjan., Oct 2, 2005
    #14
  15. Papajo

    Lee Guest

    Re: Counter Script [button disabling]

    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.
    Lee, Oct 2, 2005
    #15
  16. JRS: In article <>, dated Fri, 30
    Sep 2005 22:50:24, seen in news:comp.lang.javascript, Robi
    <> posted :
    >Dr John Stockton wrote in message news:...
    >> 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.
    Dr John Stockton, Oct 2, 2005
    #16
  17. Papajo

    Evertjan. Guest

    Re: Counter Script [button disabling]

    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)
    Evertjan., Oct 2, 2005
    #17
  18. Papajo

    Lee Guest

    Re: Counter Script [button disabling]


    >> 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".
    Lee, Oct 3, 2005
    #18
  19. Papajo

    Evertjan. Guest

    Re: Counter Script [button disabling]

    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)
    Evertjan., Oct 3, 2005
    #19
  20. Papajo

    Lee Guest

    Re: Counter Script [button disabling]

    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.
    Lee, Oct 3, 2005
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. The Eeediot
    Replies:
    3
    Views:
    2,206
    =?Utf-8?B?UnVsaW4gSG9uZw==?=
    Dec 22, 2004
  2. Mark Hobley

    Running a CGI Counter Script

    Mark Hobley, Dec 7, 2005, in forum: HTML
    Replies:
    10
    Views:
    837
    Neredbojias
    Dec 9, 2005
  3. George2
    Replies:
    1
    Views:
    778
    Alf P. Steinbach
    Jan 31, 2008
  4. T Stewart

    Problem with counter CGI script

    T Stewart, Jul 5, 2003, in forum: Perl Misc
    Replies:
    1
    Views:
    197
    Gunnar Hjalmarsson
    Jul 7, 2003
  5. Randy Webb

    word counter script locks up Mozilla

    Randy Webb, Feb 15, 2004, in forum: Javascript
    Replies:
    6
    Views:
    108
    David
    Feb 16, 2004
Loading...

Share This Page