creating elements and attaching content with dom

U

uoL

Hello !
Anybody knows why this function doesn't work ? i'ts really simple I
create a p element and try to append a text element. The only thing
that work is appending a text element to de document element. then
nothing

function addtext(what){
var theNewParagraph = document.createElement('p');
var theTextOfTheParagraph = document.createTextNode('Some content.');
theNewParagraph.appendChild(theTextOfTheParagraph);
}

Note: if I create the element div on the document and then refer to it
like this :

function addtext(what){
var theNewParagraph = document.createElement('p');
var theTextOfTheParagraph = document.createTextNode('Some content.');
document.getElementById('someElementId').appendChild(theNewParagraph);
}

it works fine.

Thanks !
Regards,
uoL
 
D

daveyand

you dont seem to append the p tag to the document.body.

Maybe its that?

so in your function just add this

document.body.appendChild(p_tag);
Might help?

Also have you looked at using innerHTML???
 
U

uoL

Hello there,

you were right ! I was so locked that I didn't see it :S I was quite
obvious ...

here is a working example:

function addtext(what){
var mydiv=document.createElement('div');
var mytext=document.createTextNode(what);
mydiv.appendChild(mytext);
}

PD: I read somewhere that is not correct (something like deprecated) to
use innerHTML, I thinks it's because it don't return a reference to the
object or something like that, but in web application you never know
what's good and what's not.

Thanks daveyand !
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top