Ranting: checkboxes are not placed in array.

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

    #1

    Ranting: checkboxes are not placed in array.

    Grrr,

    Forgive my ranting, but this really irritated me a lot.

    <ranting>
    I have a page with X checkboxes with the same name.

    code like:
    var numberofCB = document.forms. updateentitiesf orm["bla"].length;

    ok?

    What happens to my surprise:
    numberofCB now only contains the number of checkboxes with that name IF
    there are more than 1.
    Otherwise it is undefined.

    I need that for stuff like:
    for (var i=0; numberofCB<i ;i++){
    document.forms. updateentitiesf orm["bla[]"][i].checked = false;
    }

    Which fails when there is only 1 checkbox.

    I don't know who decided to start creating the array ONLY when more than 1
    element is found, but that guy forces me to start coding stupid branches.

    Why not always make it an array?

    </ranting>

    Thanks for your time.

    Regards,
    Erwin Moller
  • RobB

    #2
    Re: Ranting: checkboxes are not placed in array.

    Erwin Moller wrote:[color=blue]
    > Grrr,
    >
    > Forgive my ranting, but this really irritated me a lot.
    >
    > <ranting>
    > I have a page with X checkboxes with the same name.
    >
    > code like:
    > var numberofCB = document.forms. updateentitiesf orm["bla"].length;
    >
    > ok?
    >
    > What happens to my surprise:
    > numberofCB now only contains the number of checkboxes with that name[/color]
    IF[color=blue]
    > there are more than 1.
    > Otherwise it is undefined.
    >
    > I need that for stuff like:
    > for (var i=0; numberofCB<i ;i++){
    > document.forms. updateentitiesf orm["bla[]"][i].checked = false;
    > }
    >
    > Which fails when there is only 1 checkbox.
    >
    > I don't know who decided to start creating the array ONLY when more[/color]
    than 1[color=blue]
    > element is found, but that guy forces me to start coding stupid[/color]
    branches.

    Calm down a bit, and you'll see how silly that is.
    refToForm.eleme ntName and refToForm.eleme nts.elementName only need one
    variable slot to hold a reference to a single form control with a
    unique name - why would an array be implemented? It's only needed for
    multiple-same-named controls so, that's where you'll find it.
    [color=blue]
    > Why not always make it an array?[/color]

    Because life is hard.

    var el = document.forms. updateentitiesf orm.bla;
    if (typeof el[0] != 'undefined')
    {...got an array...

    Comment

    • Erwin Moller

      #3
      Re: Ranting: checkboxes are not placed in array.

      RobB wrote:
      [color=blue]
      > Erwin Moller wrote:[color=green]
      >> Grrr,
      >>
      >> Forgive my ranting, but this really irritated me a lot.
      >>
      >> <ranting>
      >> I have a page with X checkboxes with the same name.
      >>
      >> code like:
      >> var numberofCB = document.forms. updateentitiesf orm["bla"].length;
      >>
      >> ok?
      >>
      >> What happens to my surprise:
      >> numberofCB now only contains the number of checkboxes with that name[/color]
      > IF[color=green]
      >> there are more than 1.
      >> Otherwise it is undefined.
      >>
      >> I need that for stuff like:
      >> for (var i=0; numberofCB<i ;i++){
      >> document.forms. updateentitiesf orm["bla[]"][i].checked = false;
      >> }
      >>
      >> Which fails when there is only 1 checkbox.
      >>
      >> I don't know who decided to start creating the array ONLY when more[/color]
      > than 1[color=green]
      >> element is found, but that guy forces me to start coding stupid[/color]
      > branches.
      >
      > Calm down a bit, and you'll see how silly that is.
      > refToForm.eleme ntName and refToForm.eleme nts.elementName only need one
      > variable slot to hold a reference to a single form control with a
      > unique name - why would an array be implemented? It's only needed for
      > multiple-same-named controls so, that's where you'll find it.[/color]

      Sorry, but this gives me the penny-wise, poundfoolish feeling.
      Because somebody wants to be smart and not use an array, the rest of the
      world has to use branching in their javascript?

      Does that make sense to you?

      Really, this was a bad choice.

      [color=blue]
      >[color=green]
      >> Why not always make it an array?[/color]
      >
      > Because life is hard.[/color]

      Maybe.
      Another reason could be that the original creator was not thinking
      everything through...
      [color=blue]
      >
      > var el = document.forms. updateentitiesf orm.bla;
      > if (typeof el[0] != 'undefined')
      > {...got an array...[/color]

      Yes, I know how to handle that.
      That is unneeded code/branching in my humble opinion.

      Really, I don't want to start ranting on you, but you make a stand for a bad
      designdecision I think.

      Thanks for your response anyway!
      :-)
      :-)
      (Two smiley to make it up to you)

      Regards,
      Erwin Moller

      Comment

      • RobB

        #4
        Re: Ranting: checkboxes are not placed in array.

        Erwin Moller wrote:[color=blue]
        > RobB wrote:
        >[color=green]
        > > Erwin Moller wrote:[color=darkred]
        > >> Grrr,
        > >>
        > >> Forgive my ranting, but this really irritated me a lot.
        > >>
        > >> <ranting>
        > >> I have a page with X checkboxes with the same name.
        > >>
        > >> code like:
        > >> var numberofCB = document.forms. updateentitiesf orm["bla"].length;
        > >>
        > >> ok?
        > >>
        > >> What happens to my surprise:
        > >> numberofCB now only contains the number of checkboxes with that[/color][/color][/color]
        name[color=blue][color=green]
        > > IF[color=darkred]
        > >> there are more than 1.
        > >> Otherwise it is undefined.
        > >>
        > >> I need that for stuff like:
        > >> for (var i=0; numberofCB<i ;i++){
        > >> document.forms. updateentitiesf orm["bla[]"][i].checked = false;
        > >> }
        > >>
        > >> Which fails when there is only 1 checkbox.
        > >>
        > >> I don't know who decided to start creating the array ONLY when[/color][/color][/color]
        more[color=blue][color=green]
        > > than 1[color=darkred]
        > >> element is found, but that guy forces me to start coding stupid[/color]
        > > branches.
        > >
        > > Calm down a bit, and you'll see how silly that is.
        > > refToForm.eleme ntName and refToForm.eleme nts.elementName only need[/color][/color]
        one[color=blue][color=green]
        > > variable slot to hold a reference to a single form control with a
        > > unique name - why would an array be implemented? It's only needed[/color][/color]
        for[color=blue][color=green]
        > > multiple-same-named controls so, that's where you'll find it.[/color]
        >
        > Sorry, but this gives me the penny-wise, poundfoolish feeling.
        > Because somebody wants to be smart and not use an array, the rest of[/color]
        the[color=blue]
        > world has to use branching in their javascript?
        >
        > Does that make sense to you?
        >
        > Really, this was a bad choice.
        >
        >[color=green]
        > >[color=darkred]
        > >> Why not always make it an array?[/color]
        > >
        > > Because life is hard.[/color]
        >
        > Maybe.
        > Another reason could be that the original creator was not thinking
        > everything through...
        >[color=green]
        > >
        > > var el = document.forms. updateentitiesf orm.bla;
        > > if (typeof el[0] != 'undefined')
        > > {...got an array...[/color]
        >
        > Yes, I know how to handle that.
        > That is unneeded code/branching in my humble opinion.
        >
        > Really, I don't want to start ranting on you, but you make a stand[/color]
        for a bad[color=blue]
        > designdecision I think.
        >
        > Thanks for your response anyway!
        > :-)
        > :-)
        > (Two smiley to make it up to you)
        >
        > Regards,
        > Erwin Moller[/color]

        Here's the way I generally do it:

        var f = document.forms. updateentitiesf orm,
        ref = f.elements['bla[]'];
        if (typeof ref[0] == 'undefined')
        ref = [ref];
        for (var i = 0, l = ref.length; i < l; i++)
        ref[i].checked = false;

        Comment

        • Matt Kruse

          #5
          Re: Ranting: checkboxes are not placed in array.

          Erwin Moller wrote:[color=blue]
          > Sorry, but this gives me the penny-wise, poundfoolish feeling.
          > Because somebody wants to be smart and not use an array, the rest of
          > the world has to use branching in their javascript?[/color]

          Rather, it's easier the way it is, IMO.

          If an array was created every time for single form elements (since, you
          know, _any_ type of form element can have multiple inputs with the same
          name) then everyone's javascript code would look like

          document.forms["formname"].elements["name"][0].value

          Which is unnecessarill confusing for the 99% of the cases where there is
          only a single input with the given name.

          The best way to avoid branching every time you write your code is to use
          generalized functions which give you the correct answer every time,
          regardless of whether there are multiple inputs of the same name, if the
          type of the input changes, etc.

          At http://www.javascripttoolbox.com/validations/ I have getInputValue()
          which will handle all these cases no matter what type of input it is, and
          many people find that it makes life easier. Hope it helps.

          --
          Matt Kruse



          Comment

          Working...