March 20, 2012

appending an <object> dynamically in IE doesn't work

Question by CCrawler

I tried several methods, none seem to work:

1 (I tried this one with other elements and it doesn’t work either):

$('#container').append('<object attribute="value"></object>');

2 (I found that this way worked with IE with most elements, but no luck here) :

var newSwf = $('<object></object>').attr('attribute', 'value');
$('#container').append(newSwf); //appendTo doesn't work either

None of the above seems to have any effect on the DOM, like nothing is appending at all. I know IE works funny with the append function, but until now, I always managed to find a workaround. I have no clue with this one though. Any ideas?

EDIT:

It seems it was my mistake, something else on my code was causing the problem in IE. I’m terribly sorry, I was hiting against a wall and I thought it was some sort of .append() issue, since IE always works funny with that.

The second method worked, as it does with other elements.

Answer by Starx

As mentioned in my comment, it works on IE 8+. But in case they are not, stick to native JavaScript for this

var newobject = document.createElement('object')
newobject.setAttribute('id','objectid');
document.getElementById("container").appendChild(newobject);

Demo

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!