Just launched the new site! Please excuse the mess as it is set up. Jump to: main content, navigation or the bottom.

Selecting all select options

This example will select all options for the select box with the id 'selectbox1':-

<input type="button" name="Button" value="All" onclick="selectAll('selectbox1',true)" />

And this example will select none of the options for the select box by passing the select object and saying we don't want them all selected (false):-

 <input type="button" name="Button" value="All" onclick="selectAll(document.getElementById('selectbox2'),false)" />

The function

Just pass the ID of the element or and whether you want all of the options selected or not and it'll do the rest!

function selectAll(selectBox,selectAll) {
    // have we been passed an ID
    if (typeof selectBox == "string") {
        selectBox = document.getElementById(selectBox);
    }

// is the select box a multiple select box? if (selectBox.type == "select-multiple") { for (var i = 0; i < selectBox.options.length; i++) { selectBox.options[i].selected = selectAll; } } }

Of course, If your select box is not a multiple select, it will do nothing! If this has been of some help to you, let me know!


Comments (9)

What others have said about this post.

  • br41nless said:

  • miles said:

  • parth said:

  • Worrawat Sakulwong said:

  • Synbios said:

  • Bill Bartmann said:

  • Bill Bartmann said:

  • mahen said:

  • Knight Samar said:

Got something to say?

Join the discussion! You know how these things work; enter your details and comments below and be heard.

If you have trouble reading the code, click on the code itself to generate a new random code.

Going up? Back to the top