Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery "Object doesn't support this property or method" in Internet Explorer

I am using jQuery to add some dynamic content to a website.

I am attempting to create a new DOM element on-demand using the following code:

container = $('<div id="myContainer"></div>');

This works perfectly in Firefox and Safari, but Internet Explorer is generating an error. The IE error is: Object doesn't support this property or method

I know that jQuery is loading properly, and have tried both the jQuery and $ syntax.

Any ideas as to what might be causing this?

like image 660
B. Slavin Avatar asked Sep 14 '09 16:09

B. Slavin


3 Answers

I don't know if it can help but I fixed my problem. Basically IE doesn't want to assign jquery object to an undefined variable.

So what I did is declare this a local variable instead.

Before:

function foo() {
  bar = $('#bar');
}

After:

function foo() {
  var bar = $('#bar');
}
like image 101
Mighub Avatar answered Nov 10 '22 00:11

Mighub


If you want to add a DOM element, the code needs to be modified a bit:

$('body').append('<div id="myContainer"></div>');
// body can be whatever containing element you want to hold myContainer
$('#myContainer').html('whatever you want inside of myContainer');
like image 45
brettkelly Avatar answered Nov 09 '22 23:11

brettkelly


I had a similar problem. Internet explorer throws this error when you try to modify a global symbol. This is the case not only for reserved words. My example was:

function foo() {
    iframe = $("myDiv").append("<iframe></iframe>");
}

This solves it:

function foo() {
    var iframe = $("myDiv").append("<iframe></iframe>");
}

This, too:

function foo() {
    myIframe = $("myDiv").append("<iframe></iframe>");
}

(but the first is better style anyway)

like image 2
Andy Adiwidjaja Avatar answered Nov 10 '22 00:11

Andy Adiwidjaja