Search code examples
jquerybrowserselected

jquery incompatibility in browsers in fetching 'selected' text


I wrote my code in I.E 8:

$(".percent_hours option:selected").each(function () {
    var val = $(this).attr("text");

firefox and chrome doesn't get the selected value (empty string).

I changed the code to:

$(".percent_hours option:selected").each(function () {
    var val = $(this).attr("value");

but now I.E. doesn't get the selected value.

Is there any way to handle this problem?

Thanks!


Solution

  • When you want to get the text , use .text(), get the value use .val()