Grouping inputs and iterating in javascript

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

    Grouping inputs and iterating in javascript

    Hi there.

    I'd like to be able to group together inputs on an HTML form inside,
    say, a <div> or <fieldset> and then use javascript to iterate through
    the controls in the group and disable them. Is this possible?

    The idea is that I can create a reusable function that will take two
    arguments - a checkbox object and a group object and when the checkbox
    is ticked, the function will disable all inputs in the group.

    Thanks in advance for your help.

    Chris Beach
  • Laurent Bugnion, GalaSoft

    #2
    Re: Grouping inputs and iterating in javascript

    Hi,

    Chris Beach wrote:
    [color=blue]
    > Hi there.
    >
    > I'd like to be able to group together inputs on an HTML form inside,
    > say, a <div> or <fieldset> and then use javascript to iterate through
    > the controls in the group and disable them. Is this possible?
    >
    > The idea is that I can create a reusable function that will take two
    > arguments - a checkbox object and a group object and when the checkbox
    > is ticked, the function will disable all inputs in the group.
    >
    > Thanks in advance for your help.
    >
    > Chris Beach[/color]

    The most direct and usable way would be to use a different form for each
    group. The "group object" would then be document.formNa me.

    You can then iterate through the elements array.

    HTH,

    Laurent
    --
    Laurent Bugnion, GalaSoft
    Webdesign, Java, JavaScript: http://www.galasoft-LB.ch
    Private/Malaysia: http://mypage.bluewin.ch/lbugnion
    Support children in Calcutta: http://www.calcutta-espoir.ch

    Comment

    • Lasse Reichstein Nielsen

      #3
      Re: Grouping inputs and iterating in javascript

      cbeach@lehman.c om (Chris Beach) writes:
      [color=blue]
      > I'd like to be able to group together inputs on an HTML form inside,
      > say, a <div> or <fieldset> and then use javascript to iterate through
      > the controls in the group and disable them. Is this possible?[/color]

      If you know that the grouped elements are all input elements (and not,
      e.g., select or textarea elements), then you can access them through

      var inputCollection = divRef.getEleme ntsByTagName("i nput")

      where divRef refers to the grouping element.
      If there are textareas or select elements too, you will have to
      handle them seperatly.
      [color=blue]
      > The idea is that I can create a reusable function that will take two
      > arguments - a checkbox object and a group object and when the checkbox
      > is ticked, the function will disable all inputs in the group.[/color]


      function disableGroup(ch eckbox,groupRef ) {
      var inputs = groupRef.getEle mentsByTagName( "input");
      for (var i=0;i<inputs.le ngth;i++) {
      inputs[i].disabled = true;
      }
      // repeat for select and textarea if needed
      }

      /L
      --
      Lasse Reichstein Nielsen - lrn@hotpop.com
      Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
      'Faith without judgement merely degrades the spirit divine.'

      Comment

      Working...