Hey Dan,

> Personally, I'd use $("#mySelect")[0].length, since it uses the actual DOM
> property for the <select /> element. This should be faster than parsing out
> the option tags that are selected.
>

Yep, I like that as well.

> However, the $("#mySelect option:selected") would allow you do other things
> with those elements (such as unselect them, change their styles, etc.)
>

I still would need to loop to remove options individually though,
right?  One of the things I am trying to do is remove user selected
options with a button click on a form.  My first attempt (which works)
looked like:

$("#removeSelected").click(function()   {
        var elSel = document.getElementById('mySelect');
        for (i = elSel.length - 1; i>=0; i--) {
          if (elSel.options[i].selected && elSel.options[i].value != '') {
            elSel.remove(i);
          }
        }
});

Trying to recode the for loop for jQuery, I am wondering how to
identify whether the index of the current row is selected (versus non-
selected) so that is can be removed. You can see below what I am
trying to get at in the if statement below, which of course does not
work. So how do I reference the counter in the loop in line 2?

for (i = $("#mySelect")[0].length - 1; i>=0; i--) {
  if ($("#mySelect option:selected")[i]) {
    $("#mySelect").removeOption(i);
  }
}

As always, thanks much.

Jeff


Reply via email to