Changing value of multiple elements with same name at once?

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

    Changing value of multiple elements with same name at once?

    I am doing a form for my site and would like to enable and disable a radio
    button set depending on whether one of the choices on an outer radio button
    set is checked. How can I refer to all the inputs of the inner radio button
    set (they all share a common name) with javascript. I tried
    document.getEle mentsByNames('t hename') but it doesn't work. I know this is
    because this method returns an array which you must then refer to by a
    specific index number, unless there is another way. I would like to refer to
    all of them at once and then set their disabled status to false. Can someone
    please help. Thanks.


  • @SM

    #2
    Re: Changing value of multiple elements with same name at once?

    TheKeith a ecrit :
    [color=blue]
    > I am doing a form for my site and would like to enable and disable a radio
    > button set depending on whether one of the choices on an outer radio button
    > set is checked. How can I refer to all the inputs of the inner radio button
    > set (they all share a common name) with javascript. I tried
    > document.getEle mentsByNames('t hename') but it doesn't work. I know this is
    > because this method returns an array which you must then refer to by a
    > specific index number, unless there is another way. I would like to refer to
    > all of them at once and then set their disabled status to false. Can someone
    > please help. Thanks.[/color]

    To disable (or enable) any serie of radio button :

    function disabl(radionam e,myform,state) {
    for(var i=0;i<myform.le ngth;i++)
    if(myform.eleme nts[i].name==radionam e)
    myform.elements[i].disabled=state ;
    }

    <form>
    <input type=radio name="foo"> Shoes <br>
    <input type=radio name="foo"> Shirts <br>
    <input type=radio name="truc"> Pencils <br>
    <input type=radio name="foo"> Ties <br>
    <input type=radio name="truc"> Papers <br>
    <input type=radio name="articles"
    onclick="if(thi s.checked==true ) {
    disabl('truc',t his.form,'true' );
    disabl('foo',th is.form,'false' ); }
    else {
    disabl('truc',t his.form,'false ');
    disabl('foo',th is.form,'true') ; }"> Clothes
    <input type=radio name="articles"
    onclick="if(thi s.checked==true ) {
    disabl('truc',t his.form,'false ');
    disabl('foo',th is.form,'true') ;}
    else {
    disabl('truc',t his.form,'true' );
    disabl('foo',th is.form,'false' ); }"> Desk
    </form>

    If the 1st radio button "articles" is checked (selected)
    make state of buttons "foo" enabled and buttons "truc" disabled
    if 1st "articles" deselected : "foo" are disabled and "truc" enabled


    That wiln't work with my NC4.5 who doesn't understand 'disabled'

    --
    *************** *************** *************** *************** **
    Stéphane MORIAUX : mailto:stephane OTER-MOImoriaux@wana doo.fr
    Aide aux Pages Perso (images & couleurs, formulaire, CHP, JS)

    *************** *************** *************** *************** **


    Comment

    • Richard Cornford

      #3
      Re: Changing value of multiple elements with same name at once?

      "@SM" <stephane_moria ux@wanadoo.fr> wrote in message
      news:3FD98ECE.7 823677A@wanadoo .fr...
      <snip>[color=blue]
      > onclick="if(thi s.checked==true ) {[/color]
      <snip>

      The "checked" attribute of a radio button is boolean, that means it can
      only have one of the two values true of false. The - if - statement
      needs to resolve its expression to a boolean value, and comparisons
      always produce boolean results. So, to take a boolean value and compare
      it with a boolean value to produce a boolean result is superfluous. In
      an - if - statement is _never_ necessary to use the type converting (as
      opposed to strict) comparison with a boolean value because the result
      will always directly reflect the type-converted trueness of the variable
      operand, and if that operand was boolean to start with there is even
      less point:-

      if(this.checked ){
      ...
      }

      Richard.


      Comment

      • @SM

        #4
        Re: Changing value of multiple elements with same name at once?

        Richard Cornford a ecrit :
        [color=blue]
        > <snip>
        > very interesting considerations
        > <snip>
        >
        > if(this.checked ){
        > ...
        > }
        >
        > Richard.[/color]

        I will try it on my old browser to see what he thinks of that.

        --
        *************** *************** *************** *************** **
        Stéphane MORIAUX

        Comment

        • TheKeith

          #5
          Re: Changing value of multiple elements with same name at once?


          "@SM" <stephane_moria ux@wanadoo.fr> wrote in message
          news:3FD98ECE.7 823677A@wanadoo .fr...[color=blue]
          > TheKeith a ecrit :
          >[color=green]
          > > I am doing a form for my site and would like to enable and disable a[/color][/color]
          radio[color=blue][color=green]
          > > button set depending on whether one of the choices on an outer radio[/color][/color]
          button[color=blue][color=green]
          > > set is checked. How can I refer to all the inputs of the inner radio[/color][/color]
          button[color=blue][color=green]
          > > set (they all share a common name) with javascript. I tried
          > > document.getEle mentsByNames('t hename') but it doesn't work. I know this[/color][/color]
          is[color=blue][color=green]
          > > because this method returns an array which you must then refer to by a
          > > specific index number, unless there is another way. I would like to[/color][/color]
          refer to[color=blue][color=green]
          > > all of them at once and then set their disabled status to false. Can[/color][/color]
          someone[color=blue][color=green]
          > > please help. Thanks.[/color]
          >
          > To disable (or enable) any serie of radio button :
          >
          > function disabl(radionam e,myform,state) {
          > for(var i=0;i<myform.le ngth;i++)
          > if(myform.eleme nts[i].name==radionam e)
          > myform.elements[i].disabled=state ;
          > }[/color]


          Yeah I was just wondering if there was an easier way without having to use a
          loop. I got it going with a loop too--I guess I'll just have to stick with
          that. Thanks.


          Comment

          • @SM

            #6
            Re: Changing value of multiple elements with same name at once?

            TheKeith a ecrit :
            [color=blue]
            > "@SM" <stephane_moria ux@wanadoo.fr> wrote in message
            > news:3FD98ECE.7 823677A@wanadoo .fr...[color=green]
            > >
            > > To disable (or enable) any serie of radio button :
            > >
            > > function disabl(radionam e,myform,state) {
            > > for(var i=0;i<myform.le ngth;i++)
            > > if(myform.eleme nts[i].name==radionam e)
            > > myform.elements[i].disabled=state ;
            > > }[/color]
            >
            > Yeah I was just wondering if there was an easier way without having to use a
            > loop. I got it going with a loop too--I guess I'll just have to stick with
            > that. Thanks.[/color]

            Loop is not difficult and is instantly executed

            In my mind you cannot evite the loop way because you have
            to pass on each right element to tell it its state (dis/en abled)

            You can use a DOM function (less heavy and faster) ==>
            there is (with both : DOM and JS if DOM uninplemented)

            function disabl(radionam e,myform,state) {
            if(document.get ElementsByName)
            {
            R = document.getEle mentsByName(rad ioname);
            for(i=0;i < R.length;i++) R[i].disabled=state ;
            }
            else
            for(var i=0;i < myform.length;i ++)
            if(myform.eleme nts[i].name==radionam e)
            myform.elements[i].disabled=state ;
            }

            --
            *************** ***
            Stéphane MORIAUX

            Comment

            • Lee

              #7
              Re: Changing value of multiple elements with same name at once?

              @SM said:
              [color=blue]
              >Loop is not difficult and is instantly executed
              >
              >In my mind you cannot evite the loop way because you have
              >to pass on each right element to tell it its state (dis/en abled)
              >
              >You can use a DOM function (less heavy and faster) ==>
              >there is (with both : DOM and JS if DOM uninplemented)
              >
              >function disabl(radionam e,myform,state) {
              > if(document.get ElementsByName)
              > {
              > R = document.getEle mentsByName(rad ioname);
              > for(i=0;i < R.length;i++) R[i].disabled=state ;
              > }
              > else
              > for(var i=0;i < myform.length;i ++)
              > if(myform.eleme nts[i].name==radionam e)
              > myform.elements[i].disabled=state ;
              >}[/color]

              It's actually even more efficient than that, since elements
              of a form that share a NAME attribute can be referred to
              using array notation:

              function disabl(radionam e,myform,state) {
              for(var i=0;i<myform.el ements[radioname].length;i++){
              myform.elements[radioname][i].disabled=state ;
              }
              }

              Comment

              Working...