views:

92

answers:

2

Ok, there seems to be a problem with the JS Code for Opera browsers, as it only removes the last option tag that is selected within a multiple select tag, can someone please help me.

Here is the HTML for this:

<select id="actions_list" name="layouts" multiple style="height: 128px; width: 300px;">
    <option value="forum">forum</option>
    <option value="collapse">collapse</option>
    <option value="[topic]">[topic]</option>
    <option value="[board]">[board]</option>
</select>

Ofcourse it's within a form tag, but there's a ton more code involved with this form, but here is the relevant info for this.

Here is the JS that should handle this, but only removes the last selected option in Opera, not sure about other browsers, but it really needs to remove all selected options, not just the last selected option... argg

var action_list = document.getElementById("actions_list");
var i = action_list.options.length;
while(i--)
{
    if (action_list.options[i].selected)
    {
        action_list.remove(i);
    }
}

What is wrong with this? I can't figure it out one bit :(

Thanks :)

A: 

You can do it much easier using jQuery:

$('#actions_list option:selected').remove()
Lukasz Dziedzia
Do I need to include libraries for this to work? If so, which one(s) do I need?
SoLoGHoST
That code you posted doesn't work... arggg
SoLoGHoST
@SoLoGHoST - It works fine if you just download and use jQuery.
Gert G
@SoLoGHoST - you need to reference jquery library in your code. You can do this by inserting line: <script type="text/javascript" src="jquery-1.4.2.min.js"></script> (assuming "jquery-1.4.2.min.js" is in directory of your html file). You can download jquery files from the site I mentioned in my post: http://jquery.com.
Lukasz Dziedzia
Ok, thanks, I'm not a huge fan of jQuery, no offense, just seems a bit too complicated for me. Will eventually get there I suppose, but not yet. Thanks anyways.
SoLoGHoST
I had the same opinion on jQuery some time ago and now I can imagine building projects without this library. After short time you will notice that it is far easier the plain JS sometimes (compare 10+ line function with 1 line of jquery) but of course it is up to you what tools suits your projects the best. Anyway it's good you solved this issue;)
Lukasz Dziedzia
+1  A: 

It's easiest to do this with jQuery but it you want to do this using plain Javascript you can.

The problem you are experiencing is that when you remove an item from the options list in Opera it deselects all the selected items, so only the first is removed. A workaround is to first remember which items were selected before removing any.

var action_list = document.getElementById("actions_list");

// Remember selected items.
var is_selected = [];
for (var i = 0; i < action_list.options.length; ++i)
{
    is_selected[i] = action_list.options[i].selected;
}

// Remove selected items.
i = action_list.options.length;
while (i--)
{
    if (is_selected[i])
    {
        action_list.remove(i);
    }
}
Mark Byers
Thanks a million, never thought to do that. This was really bugging the hell out of me. Why can't they all work the same... argg... Well, that's the glory of diversity. Cheers :) Bytheway, I'm not very familiar with jQuery, as easy as it allegedly is, but I guess I'm just an old-fashioned fool and prefer JS. Thanks again! :)
SoLoGHoST