Multiple Selection Listbox Javascript Problem in MS Internet Explorer

internet-explorer-ieLast night I came across very weird problem of JavaScript. I wrote a JavaScript code to select all the option of the list. Let’s take following example for demonstration.

HTML code:

Now, as you can see, we haven’t specify “multiple” attribute of both the select tag. So, by default it treated as false. Means you cannot select multiple option from the select box. Now I wrote following JavaScript code and it’s working fine with all the major browsers like Mozilla FireFox, Safari 3 and Safari 4, Google Chrome, even in IE 7+ also but surprisingly it is not working in IE 6 (Internet Explorer 6). Situation became more weird when I place or un-commented alert(1) at line 6 and it worked correctly in Internet Explorer 6 too!!

JavaScript code:

Then I modified above javascript code a bit and tested on all the browsers including IE 6 and it was working fine. I called focus() function for the particular list box. I don't know the reason why it works fine in IE 6 when we put focus function.

Modified JavaScript code:

Get our Articles via Email. Enter your email address.

You may also like...


  1. Marcel says:

    Thanks for posting this, I was pulling my hairs out trying to get that piece of redmond shit to run my code correctly.

    THANK YOU for this post !!!!!!!!

  2. Deborah says:

    Write about more IE work-arounds for Ajax and javascript with PHP and Mysql

  3. Viral Patel says:

    Hi Marcel,
    Thanks for the comment. You may want to subscribe for RSS feed of this website to stay in touched with different technologies.

    Hi Deborah,
    Thanks for your comment. I will sure try to write about PHP/Ajax/Javascript. You may like to contribute to this website by sending your articles. It will be published under your username.

  4. BJS says:

    Nice fix. Thanks for sharing it.

  5. Thank-you so much – you’re a life saver! (Or certainly a several hours of my life saver!)

  6. Raph says:

    Thank you so much!!

  7. sridevi says:

    could never have thought of this to fix.. it works.. Like marcell, I too had almost pulled my hair on this bug..

  8. canhnv says:

    Thank you so much!!!

  9. Ashish Kulkarni says:

    Thank you so much, I’ve been tearing my hair about this bug for a day or two and your solution works. I was creating the options and selecting them (if required) in the same step, which caused another problem: the didn’t expand to fit the length of the displayed options. To solve that, I split the code into two: first create the options, apply your hack and then set the selected flag (which worked perfectly).

  10. Tristan ARNAULT says:


    Thanks for the tips.
    My problem was a little bit more complex because I was trying to do the same than you but on a select with “display:none”.
    And Internet Explorer 6 does not like focusing on hidden elements (fatal javascript error).
    This is my solution based on yours for that specific case :

    function selectAllOption(obj) {
    // obj here is an hidden select box
    var len = obj.options.length;
    obj.multiple = true; = ‘inline’; // IE6 Hack
    obj.focus(); // IE6 Hack
    for (var i=0; i < len; i++) {
    obj.options[i].selected = true;
    } = 'none'; // IE6 Hack

    So the tips is to display the select box just before focusing it, and just after hidding it again. This solution does not change the way the user sees the webpage.

    Posting this tips here in case of someone encounters the same problem :)

    See ya,

    Tristan ARNAULT

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.