Select from a list

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

    #1

    Select from a list

    Is there a way to select all the elements of a list via a JavaScript?

    This is a way it is

    <form action="" method="post" enctype="applic ation/x-www-form-urlencoded">
    <select name="select" size="6" multiple>
    <option value="TEST1">T EST1</option>
    <option value="TEST2">T EST2</option>
    <option value="TEST3">T EST3</option>
    </select>
    <P>
    <input type="button" name="Button" value="Select All">
    </form>

    I would like to end up after clicking on the button to be like this

    <form action="" method="post" enctype="applic ation/x-www-form-urlencoded">
    <select name="select" size="6" multiple>
    <option value="TEST1" selected>TEST1</option>
    <option value="TEST2" selected>TEST2</option>
    <option value="TEST3" selected>TEST3</option>
    </select>
    <P>
    <input type="button" name="Button" value="Select All">
    </form>

    Is there any way to do this.

    Thanks for any help you can give!!!

    Rick


  • RobG

    #2
    Re: Select from a list

    Rick wrote:[color=blue]
    > Is there a way to select all the elements of a list via a JavaScript?
    >
    > This is a way it is
    >
    > <form action="" method="post" enctype="applic ation/x-www-form-urlencoded">
    > <select name="select" size="6" multiple>
    > <option value="TEST1">T EST1</option>
    > <option value="TEST2">T EST2</option>
    > <option value="TEST3">T EST3</option>
    > </select>
    > <P>
    > <input type="button" name="Button" value="Select All">
    > </form>
    >
    > I would like to end up after clicking on the button to be like this
    >
    > <form action="" method="post" enctype="applic ation/x-www-form-urlencoded">
    > <select name="select" size="6" multiple>
    > <option value="TEST1" selected>TEST1</option>
    > <option value="TEST2" selected>TEST2</option>
    > <option value="TEST3" selected>TEST3</option>
    > </select>
    > <P>
    > <input type="button" name="Button" value="Select All">
    > </form>
    >
    > Is there any way to do this.
    >[/color]

    Here's your button (presumes the select has at least one option):

    <input type="button" name="Button" value="Select All" onclick="
    var o = this.form.eleme nts['select'].options;
    var i = 0;
    do {
    o[i].selected = true;
    } while ( o[++i] )
    ">



    --
    Rob

    Comment

    • Rick

      #3
      Re: Select from a list

      "RobG" <rgqld@iinet.ne t.auau> wrote in message
      news:nGUie.1525 $Zn.76543@news. optus.net.au...[color=blue]
      > Rick wrote:[color=green]
      >> Is there a way to select all the elements of a list via a JavaScript?
      >>
      >> This is a way it is
      >>
      >> <form action="" method="post"
      >> enctype="applic ation/x-www-form-urlencoded">
      >> <select name="select" size="6" multiple>
      >> <option value="TEST1">T EST1</option>
      >> <option value="TEST2">T EST2</option>
      >> <option value="TEST3">T EST3</option>
      >> </select>
      >> <P>
      >> <input type="button" name="Button" value="Select All">
      >> </form>
      >>
      >> I would like to end up after clicking on the button to be like this
      >>
      >> <form action="" method="post"
      >> enctype="applic ation/x-www-form-urlencoded">
      >> <select name="select" size="6" multiple>
      >> <option value="TEST1" selected>TEST1</option>
      >> <option value="TEST2" selected>TEST2</option>
      >> <option value="TEST3" selected>TEST3</option>
      >> </select>
      >> <P>
      >> <input type="button" name="Button" value="Select All">
      >> </form>
      >>
      >> Is there any way to do this.
      >>[/color]
      >
      > Here's your button (presumes the select has at least one option):
      >
      > <input type="button" name="Button" value="Select All" onclick="
      > var o = this.form.eleme nts['select'].options;
      > var i = 0;
      > do {
      > o[i].selected = true;
      > } while ( o[++i] )
      > ">[/color]

      WOW this worked sooo good. Thanks very much!!!

      Is there anyway to make sure the put at least one entry in the list?

      Thanks again!!!


      Comment

      • RobG

        #4
        Re: Select from a list

        Rick wrote:[color=blue]
        > "RobG" <rgqld@iinet.ne t.auau> wrote in message
        > news:nGUie.1525 $Zn.76543@news. optus.net.au...
        >[/color]
        [...][color=blue][color=green]
        >>
        >> Here's your button (presumes the select has at least one option):
        >>
        >> <input type="button" name="Button" value="Select All" onclick="
        >> var o = this.form.eleme nts['select'].options;
        >> var i = 0;
        >> do {
        >> o[i].selected = true;
        >> } while ( o[++i] )
        >> ">[/color]
        >
        >
        > WOW this worked sooo good. Thanks very much!!!
        >
        > Is there anyway to make sure the put at least one entry in the list?
        >[/color]

        To make it more robust, put the following in a script:

        function checkAll(el){
        if ( el && /select/i.test(el.nodeN ame) ){
        var o = el.options;
        var i = o.length;
        while ( i-- ) { o[i].selected = true; }
        }
        }

        And call it with:

        <input type="button" name="Button" value="Select All"
        onclick="checkA ll(this.form.el ements['select'])">

        Now you don't care if the element doesn't exist or has no options, no
        error messages will pop up. If it's not a multiple select, the first
        option will end up being selected.



        --
        Rob

        Comment

        Working...