views:

24

answers:

2

Hello,

this works great in FF but not in IE, Chrome or Safari.

$('#countryDropDown option').click(function() {
   var countryID = $(this).val();
   dostuff();
});
// countryDropDown = id of select

So, as you can see I want to attach a click event to each option.

I alos tried

var allOpts = $('#countryDropDown option'),
   l = allOpts.length,
   i = 0;

for (i = 0; i < l; i += 1) {
    $(allOpts[i]).click(function() {
        var countryID = $(this).val();
        doStuff();
    });
}

It still does not want to work in any other browser but FF. What am I doing wrong? Thanks

+2  A: 

The question is if you really need to do it this way. I bet you don't need access to the option element, so you also could use .change():

var countryID;

$('#countryDropDown').change(function() {
   countryID = $(this).val();
   dostuff();
});

Update:

According to the documentation, .val() should work, but if it does not for whatever reason, do this:

var countryID;
$('#countryDropDown').change(function() {
   countryID = $('option:selected', this).val();
   dostuff();
});

Update2:

I don't know if it is relevant (I think it should work nevertheless) but could it be that your option elements don't have a value attribute, likes so:

<option>Foo</option>

If so you should try .text() instead of .val().

$('option:selected', this).text();
Felix Kling
why don't you think I need access to the option element?I need to get the value out of it... is there any other way?maybe get the value of the select box which will get the selected options value?
meow
@meow - `$(this).val()`, in the context of a select element, gets the selected option's value. This is correct.
karim79
ps. .change() does not work in any other browser than FF, just like click..
meow
@meow: Yes that is exactly what I posted. The value of a `select` element is the current selected option. You are not manipulating the `option` element, that is what I meant that you probably don't need access to it.
Felix Kling
@meow: I doubt that. Every jQuery method works in most popular browsers. That is what jQuery is for, to not have to deal with cross browser issues. There is probably something else wrong in your JS which FF compensates. Maybe you have to post more complete code.
Felix Kling
@ karimthis does not work, if i do console.log $('#countryDropDown').change(function() { countryID = $(this).val(); console.log(countryID)});i get an empty string..
meow
@meow: Please note that I declared `countryID` before assigning the handler: `var countryID;`. Maybe this is the reason it does "not work".
Felix Kling
@ felix, no sorry, it still return an empty string..
meow
@felix, thanks for your update, i still get an empty string.will have to try something else
meow
@meow: You could try `.text()` instead if `.val()`: `$('option:selected', this).text()`
Felix Kling
@felix,the html looks like this:<select id="countryDropDown"> <option value="1520">Norway</option> <option value="1420">Finland</option> <option value="1320">Denmark</option></select>
meow
A: 

val() is used to retrieve the value of a form element. An option is not a form element but a sub-element if you will. Further more your approach will not work if they use the keyboard. You are better off doing it this way.

$("#countryDropdown").change(function() {
    var countryId = $(this).val();
    dostuff();
});
Alistair
okay, so it works, but it gives me an empty string in return instead of the value of the clicked option..
meow
If you are getting an ID then it should be in the value of the option.<option value="76">Japan</option> -> will work with val(). Agree, maybe text() if you want the option text?
Alistair