views:

130

answers:

4

what's the difference between

var div = document.createElement('div');//output -> [object HTMLDivElement]

document.getElementById('container').appendChild(div);

and

var div = '<div></div>';

document.getElementById('container').appendChild(div);//output -> <div></div>

shouldn't both be the same? and if not, how do i get the 2nd version to work?

+1  A: 

The later is pure html for an element while first is the object. For the first, you need appendChild while for the later, you need innerHTML.

shouldn't both be the same? and if not, how do i get the 2nd version to work?

var div = '<div></div>';
document.getElementById('container').innerHTML = div;
Sarfraz
ha, nice, thank you!
if you're happy with the answer you should tick it as accepted. note that `+=` is the operator that closest approximates the `append` behavior, not `=`.
David Hedlund
@user: You are welcome ....
Sarfraz
+2  A: 

appendChild is expecting an element so when you send it text, it doesn't know what to do. You might want to look into using a javascript library to ease some of that work, such as jQuery. Your code would be:

$('#container').append('<div></div>');
Dan Heberden
There _so_ needs to be a 'hold on, 2 other people are typing answers right now' message when typing up the answer, lol
Dan Heberden
@Dan Heberden: while i think it would be overhead to include jquery *only* for this, it's can still be argued that *if* OP was already using jQuery, this might very well have been the solution closest to what OP wanted, so i think it's a worthy alternative, that shouldn't be lost just because somebody else has offered a solution already.
David Hedlund
+1  A: 

appendChild really does expect a HTMLDomNode of some kind, such as a HTMLDivElement, and not a string. It doesn't know how to deal with a string. You could do

document.getElementById('container').innerHTML += div;

but I really prefer the first version; and I'd rely more on it to behave the same across browsers.

David Hedlund
+1  A: 

With your JS/DOM engine, calling Element.appendChild with a string as an argument causes a new Text node to be created then added.

Your first example creates a <div> element. Your second example creates a text node with <div></div> as its contents.

Your second example is equivalent to:

var div = '<div></div>';

document.getElementById('container').appendChild(document.createTextNode(div));//output -> <div></div>

As Sarfraz Ahmed mentioned in his answer, you can make the second example work how you want it to work by writing:

var div = '<div></div>';

document.getElementById('container').innerHTML = div;
strager
@strager: is there really any browser where passing a string argument causes a new text node to be created and added? in firefox and chrome, this just causes an illegal argument exception, and nothing is added: http://jsbin.com/omofo3/edit
David Hedlund
@David Hedlund, I was explaining the behaviour the poster was experiencing. I didn't test any browsers (I probably should have). You're right; this seems to be an exceptional case.
strager