Results 1 to 5 of 5

Thread: how to insert text or responseText to the div by using "appendChild" ?

  1. #1
    Join Date
    Aug 2008
    Location
    karanganyar, solo, indonesia
    Posts
    161
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default how to insert text or responseText to the div by using "appendChild" ?

    how to insert text or responseText to the div by using "appendChild" ?

    i have a div where i want to insert responseText to.

    <div id="container">

    <div id="div_1">
    i love music..
    i love mtv too
    </div>

    <div id="div_2">
    i love you...
    i love my self tooo...
    </div>

    </div>

    MY problem here is,
    i don't know how to insert "text/ responseText" to the place before <div id="div_1">

    if i use this code, it will insert the "text" after <div id="div_2">i love you...i love my self tooo...</div>


    code:

    var d = document.createElement('DIV');
    d.innerHTML = myajax.responseText;
    document.getElementById('container').appendChild(d);

    can anyone help?
    ///////////////////////////////////////////////////
    ///// http://www.mediatutorial.web.id
    ///////////////////////////////////////////////////

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,069
    Thanks
    44
    Thanked 3,216 Times in 3,178 Posts
    Blog Entries
    12

    Default

    Code:
    var d = document.createElement('DIV');
    d.innerHTML = myajax.responseText;
    document.getElementById('container').insertBefore(d, document.getElementById('div_1'));
    Or, if you want to always insert to the beginning regardless of what's there:

    Code:
    var d = document.createElement('DIV');
    d.innerHTML = myajax.responseText;
    document.getElementById('container').insertBefore(d, document.getElementById('container').firstChild);
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Aug 2008
    Location
    karanganyar, solo, indonesia
    Posts
    161
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default yeah

    yeah!
    thanks, i'm new in ajax and javascript, so i don't know about "insertBefore"
    thanks
    ///////////////////////////////////////////////////
    ///// http://www.mediatutorial.web.id
    ///////////////////////////////////////////////////

  4. #4
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    insertBefore essentially allows the developer, you, to place an element anywhere on the page. It takes two parameters, the first is the element that you wish to place, and the second is the placeholder or the element you wish to insert before.

    therefore,
    Code:
    document.getElementById('container').insertBefore(d, document.getElementById('container').firstChild);
    states that it will be inserting the element defined by the variable d

    Code:
    var d = document.createElement('DIV');
    d.innerHTML = myajax.responseText;
    and it is being placed before the first element of the container element as defined by

    Code:
    document.getElementById('container').firstChild

  5. #5
    Join Date
    Aug 2008
    Location
    karanganyar, solo, indonesia
    Posts
    161
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default thnsk very much

    thanks you very much
    ///////////////////////////////////////////////////
    ///// http://www.mediatutorial.web.id
    ///////////////////////////////////////////////////

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •