creating elements and attaching content with dom

Discussion in 'Javascript' started by uoL, Nov 7, 2006.

  1. uoL

    uoL Guest

    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
    uoL, Nov 7, 2006
    #1
    1. Advertising

  2. uoL

    daveyand Guest

    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???

    On Nov 7, 12:43 pm, "uoL" <> wrote:
    > 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
    daveyand, Nov 7, 2006
    #2
    1. Advertising

  3. uoL

    uoL Guest

    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 !


    On 7 nov, 10:07, "daveyand" <> wrote:
    > 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???
    >
    > On Nov 7, 12:43 pm, "uoL" <> wrote:
    >
    > > 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
    uoL, Nov 7, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Seberino
    Replies:
    3
    Views:
    734
    Stefan Behnel
    Jun 12, 2009
  2. Csaba2000

    attaching a method to DOM elements

    Csaba2000, Mar 7, 2005, in forum: Javascript
    Replies:
    2
    Views:
    88
    Michael Winter
    Mar 7, 2005
  3. dd
    Replies:
    8
    Views:
    125
  4. Replies:
    4
    Views:
    89
  5. Replies:
    4
    Views:
    105
Loading...

Share This Page