Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

click event for option doesn't work in IE

Tags:

jquery

onclick

I have a multiple select tag, and I need to write the function onclick of it's options, because I need to get the value of last clicked option, but when I wrote the following

$("#multiple_select option").click(function()
{
     var val = $(this).val();
     alert(val);
});

it doesn't work in IE.

What is the problem?

UPDATE

I need exactly click event, because I wrote a function onclick event already(demo here), and I need to fix last changed element's value, which is impossible to make without click event(I think)

like image 604
Simon Avatar asked Jul 27 '10 08:07

Simon


2 Answers

don't bind it on option

$("#multiple_select").click(function(){
     alert("works");
});

accepted answer:

$(document).ready(function()
{
    var options = $("#supply_cities_select :selected");
    var lastOption;
    $("#supply_cities_select").click(function()
        {
            lastOption = $(this).find(':selected').not(options);
            options = $(this).find(':selected');
        })
});
like image 79
Reigel Avatar answered Sep 24 '22 13:09

Reigel


If you really want to have a click event on each option, you need to have List instead of a dropdown style.

To accomplish that, add the size attribute into the select element for instance:

<select type="multiple" size=4>
  <option>foo</option>
  <option>bar</option>
  <option>baseball</option>
</select>​​​​​​​​​​​​​​​​​​​​​​​​​

Now you can bind each option individually.

If you want to get the value of a clicked option use the change event handler and .val() method, like:

$("#multiple_select").change(function() {
  var val = $(this).val();
  alert(val);
});
like image 24
jAndy Avatar answered Sep 21 '22 13:09

jAndy