views:

2970

answers:

5

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?

+6  A: 

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');
inkedmn
Still not sure why the original method wasn't working (since the jQuery docs list it), but your method was a perfect alternative. Thanks.
B. Slavin
Yep, tested and works for me, too. Thanks, inkedmn!
Dick Lampard
A: 

I have the same and I'm not creating a DOM element. I'm just trying to assign a jQuery object to a global variable.

Silly IE.

mig

Mighub
+3  A: 

Hello guys,

I don't know if it can help but I fixed my problem. Basically IE doesn't want to assign jquery object to a global variable. It was in a function.

So what I did is declare this local variable on top of it instead.

Before:

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

After:

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

Not crazy but it makes this f***g explorer shut the f**k up.

Bye mig

Mighub
Worked for me.you might saved me lots of frustration :) thanks
TheOsp
Perfect, worked for me too.
Roy
A: 

I came across a very similar problem yesterday. It seems the Internet Explorer creates a lot of global variables and you run into problems when you try to overwrite them. "container" sounds very generic. Can you try using a different name? I have no testmachine here

Tobo
A: 

try once more by renaming your variable.

I got the result by doing that.

thanks Zia

zia