SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict The Mog's Avatar
    Join Date
    Dec 2002
    Location
    Manchester UK
    Posts
    310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Select all - desellect all

    I am sure this is a pretty easy thing to do for a javascript guru,

    i have a select list, and want to select all options using a checkbox.

    i have it working with 2 check boxes but how do i make it so that if the box is selected it selects all and deselected it deselects all.

    I am posting the code i have that works so far, in case it helps.

    Also being a php programmer i know nothing about js so you will really need to spell this out for me

    HTML Code:
    function selectall() {
     aOptions = document.forms['f'].city.options;
     if(aOptions.length) {
      for(var i=0; i<aOptions.length; i++)
       aOptions[i].selected = 'selected';
     }
    }
    function deselectall(){
     aOptions = document.forms['f'].city.options;
     if(aOptions.length) {
      for(var i=0; i<aOptions.length; i++)
       aOptions[i].selected = false;
     }
    }
     
    <!-- and the checkboxes in question -->
     
    <input type="checkbox" name="selectall" value="selectall" onClick=selectall();>
    <input type="checkbox" name="deselectall" value="deselectall" onClick=deselectall();>
     
    Cheers

    K-

  2. #2
    SitePoint Member
    Join Date
    Jun 2004
    Location
    Dunedin
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    do you mean to do this? i hope this helps

    cheers


    <html>
    <head>
    <script>
    function CheckSelected() {
    var s1 = document.getElementById("selectBox1");
    if(s1.status == true){
    selectall();
    }else if(s1.status == false){
    deselectall();
    }
    };

    </script>
    </head>
    <!-- and the checkboxes in question -->

    <input type="checkbox" id="selectBox1" value="selectall" onClick=CheckSelected();>


    </html>

  3. #3
    SitePoint Addict
    Join Date
    May 2004
    Location
    Europe
    Posts
    216
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Or all stuffed together:
    Code:
    function selectDeselect(cbox) {
     var aOptions = document.forms['f'].city.options;
     for(var i=0; i<aOptions.length; i++) {
       aOptions[i].selected = cbox.checked;
     }
    }
    and <input type="checkbox" onClick="selectDeselect(this)">


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •