can't get checkbox to check

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • LRW

    #1

    can't get checkbox to check

    Very basic, very simple, but I can't get a checkbox to check when the
    value of a text field changes to something greater than 0.

    Here's what I have:

    THE JAVASCRIPT:

    <script language=JavaSc ript>
    <!-- Begin
    function sel_comm(comm_v al)
    {
    if (comm_val > 0)
    {
    document.inhous e-f2.add_comm.che cked = true;
    }
    }
    // End -->
    </script>

    HTML:

    <INPUT TYPE="checkbox" NAME="add_comm" ID="add_comm" VALUE="1">
    Commission: <INPUT TYPE="text" NAME="item_comm " ID="item_comm"
    size="6" onChange="sel_c omm(this.value) ;">

    I just don't get why it's not working. I must be missing something,
    but I have no idea what. The FORM tag's NAME and ID elements are
    "inhouse-f2". What else could it be?

    Thanks for any ideas!
    Liam
  • kaeli

    #2
    Re: can't get checkbox to check

    In article <4c3472cb.04093 01050.393524db@ posting.google. com>,
    mechphisto@gmai l.com enlightened us with...[color=blue]
    > Very basic, very simple, but I can't get a checkbox to check when the
    > value of a text field changes to something greater than 0.
    >[/color]

    Actually, not as simple as you'd think.

    Textboxes are text. You're comparing text to a number. It's trying to convert
    to compare, but what it does it is converts the number to text, so you get
    the wrong kind of comparison.

    [color=blue]
    > Here's what I have:
    >
    > THE JAVASCRIPT:
    >
    > <script language=JavaSc ript>[/color]

    type="text/javascript"
    language is deprecated.
    [color=blue]
    > <!-- Begin
    > function sel_comm(comm_v al)
    > {
    > if (comm_val > 0)[/color]

    if (parseInt(comm_ val,10) > 0)
    [color=blue]
    > <INPUT TYPE="checkbox" NAME="add_comm" ID="add_comm" VALUE="1">[/color]
    Commission: <INPUT TYPE="text" NAME="item_comm " ID="item_comm"
    size="6" onChange="sel_c omm(this.value) ;">

    Also note that the onChange event fires AFTER focus has left the element. I
    don't know how good it is to use the 'this' keyword across browsers for that.
    Anyone have any comments about that?

    Oh, and I think value is undefined if there is no value in the box. So test
    what happens if the user deletes the value and then moves focus. You may have
    to test for 'not a number' conditions, too.

    And your syntax in your function isn't cross-browser, either. If this is for
    the internet, your best bet is
    document.forms["inhouse-f2"].elements["add_comm"].checked = true;

    HTH

    --
    --
    ~kaeli~
    In democracy your vote counts. In feudalism your count votes.



    Comment

    • Michael Winter

      #3
      Re: can't get checkbox to check

      On Thu, 30 Sep 2004 14:32:47 -0500, kaeli <tiny_one@NOSPA M.comcast.net>
      wrote:
      [color=blue]
      > In article <4c3472cb.04093 01050.393524db@ posting.google. com>,
      > mechphisto@gmai l.com enlightened us with...[/color]

      [snip]
      [color=blue]
      > Textboxes are text. You're comparing text to a number. It's trying to
      > convert to compare, but what it does it is converts the number to text,
      > so you get the wrong kind of comparison.[/color]

      No. Comparisons, with both relational and equality operators, are biased
      towards numbers. If one operator is a number and the other is string, the
      string is converted to a number, even if that conversion results in NaN.

      [snip]
      [color=blue][color=green]
      >> <!-- Begin[/color][/color]

      Don't try to hide scripts. It's not necessary any more. If you have some
      special requirement, such as the page is parsed by software that can't
      cope with scripts, place the script in an external file. Most scripts
      should be separated from the document anyway.
      [color=blue][color=green]
      >> function sel_comm(comm_v al)
      >> {
      >> if (comm_val > 0)[/color]
      >
      > if (parseInt(comm_ val,10) > 0)[/color]

      So in light of the above, that isn't needed. It won't do any harm, but
      it's redundant. If some bugs I don't know of apply to such a comparison,
      using

      if((+comm_val) > 0) {

      would be more efficient.
      [color=blue][color=green]
      >> <INPUT TYPE="checkbox" NAME="add_comm" ID="add_comm" VALUE="1">[/color]
      >
      > Commission: <INPUT TYPE="text" NAME="item_comm " ID="item_comm"
      > size="6" onChange="sel_c omm(this.value) ;">
      >
      > Also note that the onChange event fires AFTER focus has left the
      > element. I don't know how good it is to use the 'this' keyword across
      > browsers for that. Anyone have any comments about that?[/color]

      The this operator always refers to the element targeted by the event. The
      only exception I know of is when the proprietary attachEvent method is
      used. Here, IE doesn't set the this operator correctly, so it refers to
      the global object, instead.

      If you want to add multiple event listeners in a cross browser way, you
      can try:

      <URL:http://www.mlwinter.pw p.blueyonder.co .uk/clj/dom-events.js>

      It hasn't displayed any problems, but do let me know if you use it and
      find any.
      [color=blue]
      > Oh, and I think value is undefined if there is no value in the box. So
      > test what happens if the user deletes the value and then moves focus.
      > You may have to test for 'not a number' conditions, too.[/color]

      It should just be an empty string. In that case, the result of the
      conversion will be 0.
      [color=blue]
      > And your syntax in your function isn't cross-browser, either. If this is
      > for the internet, your best bet is
      > document.forms["inhouse-f2"].elements["add_comm"].checked = true;[/color]

      It's not just "good form" in this case; it's required.

      document.inhous e-f2.add_comm.che cked = true

      should be treated thus:

      1) f2.add_comm.che cked is subtracted from document.inhous e
      2) true is assigned to the result of the operation

      Both steps are errors. "f2" and "inhouse" don't exist, so that will stop
      execution of the script. If it somehow succeeded, the second step would
      fail as a subtraction isn't a valid left-hand expression. This is the
      source of the problem.

      Mike

      --
      Michael Winter
      Replace ".invalid" with ".uk" to reply by e-mail.

      Comment

      • kaeli

        #4
        Re: can't get checkbox to check

        In article <opse5o2suox13k vk@atlantis>, M.Winter@blueyo nder.co.invalid
        enlightened us with...[color=blue]
        >
        > No. Comparisons, with both relational and equality operators, are biased
        > towards numbers. If one operator is a number and the other is string, the
        > string is converted to a number, even if that conversion results in NaN.
        >[/color]

        Then is parseInt really only needed when BOTH operands are strings and you
        want a number?

        TIA

        --
        --
        ~kaeli~
        The definition of a will?... (It's a dead giveaway.)



        Comment

        • Michael Winter

          #5
          Re: can't get checkbox to check

          On Fri, 1 Oct 2004 08:39:17 -0500, kaeli <tiny_one@NOSPA M.comcast.net>
          wrote:

          [snip]
          [color=blue]
          > Then is parseInt really only needed when BOTH operands are strings and
          > you want a number?[/color]

          Pretty much, yeah, otherwise you'd perform a lexical string comparison.
          null and false convert to 0, true converts to 1, and undefined converts to
          NaN. Objects also attempt to convert to a number, but if they can't, I
          doubt forcing a conversion would do any good.

          Mike

          --
          Michael Winter
          Replace ".invalid" with ".uk" to reply by e-mail.

          Comment

          Working...