Jump to content

createElement


phorcon3

Recommended Posts

can someone please tell me what im doin wrong? it seems to be workin fine in IE but not FF ...so ..obviously it must be crappy codin, since only IE accepts that sorta codin

 

var div0 = document.createElement('div');

var div1 = document.createElement('<div class="class1"></div>');

var div2 = document.createElement('div');

var text = document.createTextNode('test');

div2.appendChild(text);

var div3 = document.createElement('<div class="class3"></div>');

div0.appendChild(div1);

div0.appendChild(div2);

div0.appendChild(div3);

var body = document.getElementsByTagName('body')[0];

body.appendChild(div0);

Link to comment
Share on other sites

There are some problems in your code, but the problem is that IE sucks and accepts your bad code. If IE was better designed in the first place, your code wouldn't have worked in the first place, and you would have realized then. So dont worry about it too much.

 

Anyways, you cannot create an element with classes the way you did. Here is how you do it in IE:

 

var div1 = document.createElement('div');
div1.className = "class1";

 

and here is how you do it in FF:

 

var div1 = document.createElement('div');
div1.setAttribute("class","class1")

 

What you have to do is create an if/else statement that identifies whether the user is using IE or FF, and then uses the proper code. I'll leave it to you to hit google and figure out how to do that - its the easy part!

Link to comment
Share on other sites

But its not in line with correct DOM standards, and may lose the ability to be backwards compatible in future browser releases. Correct DOM standards use setAttribute("class", "classname"). Therefore its better to use this, and then use a fix for IE which doesn't meet standards.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.