var e = form.elements;

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

    var e = form.elements;

    Hi Gurus

    I have the following statement in my code:

    var e = form.elements;

    Is it possible to put any conditions on this statement. What I actually want to say is:

    var e = form.elements BUT ONLY FOR CHECKBOXES THAT START WITH R.

    IN that way <INPUT TYPE="checkbox" NAME="r23"> would be part of e, but <INPUT TYPE="submit">, etc... would not be included



    TIA



    - Nicolaas


  • RobB

    #2
    Re: var e = form.elements;

    WindAndWaves wrote:[color=blue]
    > Hi Gurus
    >
    > I have the following statement in my code:
    >
    > var e = form.elements;
    >
    > Is it possible to put any conditions on this statement. What I[/color]
    actually want to say is:[color=blue]
    >
    > var e = form.elements BUT ONLY FOR CHECKBOXES THAT START WITH R.
    >
    > IN that way <INPUT TYPE="checkbox" NAME="r23"> would be part of e,[/color]
    but <INPUT TYPE="submit">, etc... would not be included[color=blue]
    >
    >
    >
    > TIA
    >
    >
    >
    > - Nicolaas[/color]


    var e = form.elements

    ....simply creates a reference to the elements (array) object of,
    presumably, a specific form. Object references, naturally, have no
    conditions - they only indicate a memory address where data is stored.
    What you need to do is 1) create a custom collection (a subset of
    Form.elements[]) filtered for only the elements you desire, or 2)
    filter at run-time to exclude unwanted controls.

    1)

    var els = document.forms[0].elements,
    rBoxes = [], //collection
    el,
    i = 0;
    while (el = els[i++])
    if (el.type == 'checkbox'
    && /^r/i.test(el.name) )
    rBoxes.push(el) ;
    2)

    if (el.type == 'checkbox'
    &&/^r/i.test(el.name) )
    {...do something with it

    Comment

    • WindAndWaves

      #3
      Re: var e = form.elements;


      "RobB" <ferndoc9@hotma il.com> wrote in message news:1107734964 .841295.99380@c 13g2000cwb.goog legroups.com...[color=blue]
      > WindAndWaves wrote:[color=green]
      > > Hi Gurus
      > >
      > > I have the following statement in my code:
      > >
      > > var e = form.elements;
      > >
      > > Is it possible to put any conditions on this statement. What I[/color]
      > actually want to say is:[color=green]
      > >
      > > var e = form.elements BUT ONLY FOR CHECKBOXES THAT START WITH R.
      > >
      > > IN that way <INPUT TYPE="checkbox" NAME="r23"> would be part of e,[/color]
      > but <INPUT TYPE="submit">, etc... would not be included[color=green]
      > >
      > >
      > >
      > > TIA
      > >
      > >
      > >
      > > - Nicolaas[/color]
      >
      >
      > var e = form.elements
      >
      > ...simply creates a reference to the elements (array) object of,
      > presumably, a specific form. Object references, naturally, have no
      > conditions - they only indicate a memory address where data is stored.
      > What you need to do is 1) create a custom collection (a subset of
      > Form.elements[]) filtered for only the elements you desire, or 2)
      > filter at run-time to exclude unwanted controls.
      >
      > 1)
      >
      > var els = document.forms[0].elements,
      > rBoxes = [], //collection
      > el,
      > i = 0;
      > while (el = els[i++])
      > if (el.type == 'checkbox'
      > && /^r/i.test(el.name) )
      > rBoxes.push(el) ;
      > 2)
      >
      > if (el.type == 'checkbox'
      > &&/^r/i.test(el.name) )
      > {...do something with it
      >[/color]
      Cool thanks, I will push them I think. Great. Thanks for your help.


      Comment

      • Grant Wagner

        #4
        Re: var e = form.elements;

        "WindAndWav es" <access@ngaru.c om> wrote in message
        news:f%FNd.1572 4$mo2.1234540@n ews.xtra.co.nz. ..[color=blue][color=green]
        >> var els = document.forms[0].elements,
        >> rBoxes = [], //collection
        >> el,
        >> i = 0;
        >> while (el = els[i++])
        >> if (el.type == 'checkbox'
        >> && /^r/i.test(el.name) )
        >> rBoxes.push(el) ;
        >> 2)
        >>
        >> if (el.type == 'checkbox'
        >> &&/^r/i.test(el.name) )
        >> {...do something with it
        >>[/color]
        > Cool thanks, I will push them I think. Great. Thanks for your help.[/color]

        If you know what you want to do with the form elements right away, then
        why pass through the elements collection once, then pass through the
        list of matching elements a second time?

        var f;
        if ((f = document.forms) &&
        (f = f['yourForm']) &&
        (f = f.elements))
        {
        var ii = f.length;
        while (ii-- > 0)
        // or
        // for (var ii = 0; i < f.length; ++ii)
        // if direction matters
        {
        if (f[ii].type == 'checkbox' &&
        /^r/i.test(f[ii].name))
        {
        // do something with f[ii]
        }
        }
        }

        --
        Grant Wagner <gwagner@agrico reunited.com>
        comp.lang.javas cript FAQ - http://jibbering.com/faq


        Comment

        • WindAndWaves

          #5
          Re: var e = form.elements;


          "Grant Wagner" <gwagner@agrico reunited.com> wrote in message news:qaMNd.179$ 3d3.550@news2.m ts.net...[color=blue]
          > "WindAndWav es" <access@ngaru.c om> wrote in message
          > news:f%FNd.1572 4$mo2.1234540@n ews.xtra.co.nz. ..[color=green][color=darkred]
          > >> var els = document.forms[0].elements,
          > >> rBoxes = [], //collection
          > >> el,
          > >> i = 0;
          > >> while (el = els[i++])
          > >> if (el.type == 'checkbox'
          > >> && /^r/i.test(el.name) )
          > >> rBoxes.push(el) ;
          > >> 2)
          > >>
          > >> if (el.type == 'checkbox'
          > >> &&/^r/i.test(el.name) )
          > >> {...do something with it
          > >>[/color]
          > > Cool thanks, I will push them I think. Great. Thanks for your help.[/color]
          >
          > If you know what you want to do with the form elements right away, then
          > why pass through the elements collection once, then pass through the
          > list of matching elements a second time?
          >
          > var f;
          > if ((f = document.forms) &&
          > (f = f['yourForm']) &&
          > (f = f.elements))
          > {
          > var ii = f.length;
          > while (ii-- > 0)
          > // or
          > // for (var ii = 0; i < f.length; ++ii)
          > // if direction matters
          > {
          > if (f[ii].type == 'checkbox' &&
          > /^r/i.test(f[ii].name))
          > {
          > // do something with f[ii]
          > }
          > }
          > }
          >
          > --
          > Grant Wagner <gwagner@agrico reunited.com>
          > comp.lang.javas cript FAQ - http://jibbering.com/faq
          >
          >[/color]

          Thank you Grant.


          Comment

          Working...