Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IE 9 jQuery error: "Object doesn't support ... 'on'"?

I need help. I'm getting the following error in IE9. The code is working in FireFox:

SCRIPT438: Object doesn't support property or method 'on' 

This is raised on the following code:

$(function() {
    $(document).on('change', '#dropdownval select', function(event) {
        //logic function
    });
});
like image 232
user1433824 Avatar asked Jun 05 '12 06:06

user1433824


1 Answers

Works in Firefox, not IE?

I would make sure that both browsers are loading the same source. To be honest, it's a bit impossible for Firefox to use .on while using jQuery 1.6, since the method didn't exist.

Within your console (F12 Developer Tools in IE and Firebug in Firefox) type the following:

jQuery.fn.jquery

This should resturn the current version of jQuery loaded on that particular page. For instance, as of today, running that command here on StackOverflow results in "1.7.1". Secondly, to test for the presence of the .on method, you can access it without its parenthesis, using double-negation to cast it to a true boolean:

!!jQuery.fn.on // True if .on is present, False otherwise

With jQuery 1.6, use .delegate rather than .on

Since you're using jQuery 1.6, you don't have access to the .on method, which was introduced in jQuery 1.7. The appropriate fallback would be to use the .delegate method instead, or you could upgrade to the latest version of jQuery (Microsoft CDN, Google CDN, jQuery CDN).

The syntax for .delegate follows that of .on pretty closely:

$("#dropdownval").delegate("select", "change", function(event){
    alert( $(this).val() );
});

Fiddle: http://jsfiddle.net/jonathansampson/rMBn4/

If you decide to upgrade...

You were close with your .on code, but you don't want to bind the event as far down as the document object - this would mean the event would need to propagate a potentially long distance before being handled. Instead, as we did with the .delegate example, we'll bind to something closer to the select element:

$("#dropdownval").on("change", "select", function(event){
    alert( $(this).val() );
});

Fiddle: http://jsfiddle.net/jonathansampson/rMBn4/1/

You'll note that the main difference between the two is the order of the first two parameters to the chained method. With .delegate, the selector precedes the event. With .on, the order is reversed.

like image 77
Sampson Avatar answered Sep 26 '22 20:09

Sampson