views:

373

answers:

3

Hello,

I currently have the following js code

function clearMulti(option)
{
    var i;
    var select = document.getElementById(option.parentNode.id);
    for(i=1;i<select.options.length;i++)
         {
        select.options[i].selected=false;
    }
}

and

function clearAllOpt(select)
{
    select.options[0].selected = false;
}

The first one deselects all options in the multiple select when called and the second clears the first option whenever anything else is selected.

The need for this is that the first option is for All.

This all works fine and dandy in FF, but in IE8 nothing happens... any suggestions on how to get this to work in both?

This is called from a jsp page... code below -- edits were made for how ids and things are populated since it's database info and other things that I probably shouldn't give out :) but this should give you the info that you're looking for.

<select id="blahSelect" name="blahSelect" style="width:80%" size=7 multiple>
     <option id="All Blah" onclick="clearMulti(this)">All Blah</option>
     <option id="**from sql**" onclick="clearAllOpt(this.parentNode)">**from sql**</option>
</select>

Thanks in advance.

A: 

There are a few issues with this code

1.) document.getElementById(option.parentNode.id);

Why are you getting the element by id when you already have the element? You should just do var select = option.parentNode

2.) I wouldn't name the variable select - name it something more appropriate. Its not reserverd but its semi-reserved as it is used in a lot of objects. So - var mySelectBoy = option.parentNode

Tim
Thanks for that, changes made... still same problem though.
Shaded
I took a look at this in IE7, and it seemed to work fine, but not 8. Maybe there is something else going on elsewhere in your code. Maybe you have something else going on in you code. A JS error somewhere.
Tim
+1  A: 

Instead of a separate onclick="" for each option, try an onchange="" on the select:

document.getElementById("bar").onchange=function(){
    if(this.options.selectedIndex > 0){
        /* deselect this.options[0] */
    }else{
        /* deselect this.options[1-n] */
    }
}

and in the HTML:

<select id="bar">
    <option>ALL</option>
    <option>option 1</option>
    <option>option 2</option>
    ...
    <option>option n</option>
</select>
greim
This seems like a solid method and works on IE8, however, is there anyway I could have it deselect the all option if the user holds ctrl and selects an option (other than all) while all is also selected? currently it will just ignore the second option selected and keep all selected. Thanks!
Shaded
I don't know offhand. One thing to try: instead of using selectedIndex, try looping through the options array and seeing which ones are .selected and basing your action on that.
greim
+1  A: 

IE8 does not fire onclick events on option elements.

Also note that selectedIndex returns the first selected option, and does not change according to the last selected option. This leads to issues when ALL is checked, and you try to check other options with CTRL held down.

You could try something like this:

<script type="text/javascript">
function excludeFirstOption(select) {

    if (select.selectedIndex === 0) {
        for (var i=1; i<select.options.length; i++) {
            select.options[i].selected = false;
        }
    }
}
</script>

<select size="7" multiple="multiple" onchange="excludeFirstOption(this)">
     <option>All</option>
     <option>Item 1</option>
     <option>Item 2</option>
     <option>Item 3</option>
     <option>Item 4</option>
</select>
thomask
Good point, have an upvote.
greim