SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast user9's Avatar
    Join Date
    Jan 2005
    Location
    localhost
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    appendChild reverse order

    Hi everyone

    I have the following code

    Code:
    function insrt(lastData)
    {
    var para=document.createElement('P');
    
    var lastData=document.createTextNode(lastData);
    
    out=document.getElementById('out'); //out is a div
    
    para.appendChild(lastData);
    out.appendChild(para);
    }
    The function works, but instead of the contents of lastData being stacked from oldest to most recent, I'd like the newest data to be on top and the oldest at the bottom. How would I change my function to achieve the desired result?

    On the following page http://www.howtocreate.co.uk/tutoria...?tut=0&part=25
    I found a function called insertBefore, however, I'm not quite sure how and if I can use it with my current function.

    Any help will be greatly appreciated.

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    insertBefore(new node, target node);

    So, you can do this:

    out.insertBefore(para, out.firstChild);

    Also, you may have problems with this statement:
    Code:
    function insrt(lastData)
    {
    
         ....
         var lastData=document.createTextNode(lastData);
    }
    A function parameter creates a local variable in which to store the argument you send to the function. You named your function parameter 'lastData', so that creates a local variable called lastData--but then you also explicitly declared a local variable named lastData in the body of the function.

  3. #3
    SitePoint Enthusiast user9's Avatar
    Join Date
    Jan 2005
    Location
    localhost
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    7stud, thank you so much, you solved my problem. I've been trying to find a solution for hours now.

    I'll also change the function parameter to something else than lastData.

    Thanks again.

  4. #4
    SitePoint Addict markchivs's Avatar
    Join Date
    Oct 2004
    Location
    Malvern Hills, UK
    Posts
    233
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Youn could just prefixes to the code without changing the rest of the variable name if you like. You know whether your dealing with nodes (nd...) or strings (s...) then too :

    Code:
    function insrt(sLastData)
    {
    var ndPara=document.createElement('P');
     
    var ndLastData=document.createTextNode(sLastData);
     
    ndOut=document.getElementById('out'); //out is a div
     
    ndPara.appendChild(ndLastData);
    ndOut.appendChild(ndPara);
    }



  5. #5
    SitePoint Enthusiast user9's Avatar
    Join Date
    Jan 2005
    Location
    localhost
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot markchivs, that's a good idea

    Easy to remember which variables are related using your method.


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
  •