tags:

views:

52

answers:

6

I have a multiple select tag, and i need to write the function onclick of it's options, becouse 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.

Whant is the problem?

Thanks

UPDATE

i need exactly click event, because i wrote a fuction onclick event allready(demo here), and i need to fix last changed element's value, which is inpossible to make without click event(i think)

+2  A: 

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');
        })
});
Reigel
i need to bind on option, becouse i need to get clicked option's value, look at update please
Syom
this would just fire each time you click on the `select` element, you need to use a `change event handler`
jAndy
This would work fine. You just need to check if the element the was click was an option (e.target.tagName == "OPTION"). Google event delegation for more.
edeverett
@edeverett: either I'm completley off or that is just wrong. Would you mind to post an example of what you mean?
jAndy
@Syom please take a look... almost got it... http://jsfiddle.net/jeDTe/1/
Reigel
@jAndy I'm wrong. Of course, if the click event isn't supported on the option elements, just moving the event binding won't help! However, in investigating this I've found an answer.
edeverett
+1  A: 

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);
});
jAndy
as I thought of it, the OP wanted to get the last clicked element option
Reigel
@Reigel exactly. i need it. look at update please
Syom
A: 
$("#multiple_select").click(function(){
  alert($(this).children("option:selected").val());
});

should to the thing

Kaaviar
and what about, if i have many selected elements?
Syom
My fault, I didn't get you could get several options.
Kaaviar
A: 
<select id="multiple_select" size="4">
    <option value="1">One</option>
    <option value="2">Two</option>
    <option value="3">Three</option>
    <option value="4">Four</option>
</select>

<script src="Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>
<script type="text/javascript">

    $(document).ready(function() {
        $('#multiple_select').click(function() {
            alert($(this).val());
        });
    });

</script>

The alert window, depending on the choice you made, shows the selected value.

XIII
A: 

Use JQuery focus() on option, rather than click().

Happy Coding.

simplyharsh
A: 

Ok, here's some IE weirdness:

Using IE, in the click event function, event.srcElement.value gives the value of the last clicked option.

Try this: http://jsfiddle.net/Ch2DT/ (only tested in IE8, needs work to make it cross browser)

edeverett