Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    May 2006
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    wrapping tags in container tags

    Hi,

    I'm trying to wrap all <li> tags within a <div> tag for a drag-drop list
    script.
    This is because of i.e's serious problems rendering list elements as objects
    in their own right (http://www.satzansatz.de/cssd/onhavinglayout.html).

    At the moment we're using .php to do this, but I'd like the script to work
    regardless of whether the content is dynamic or not.
    The obvious solution is to use the DOM to traverse the page and wrap any
    <li> tags within <div> tags

    but I'm having problems actually implementing the script in a way that the
    li elements are copied with all their attributes (onmouseover, onmouseout
    etc) intact, in the order they are currently in the list
    Here's where I stand ... any ideas?


    function wrapLI_elements(divID) {

    // divID is HTMLobject

    UL_array = divID.getElementsByTagName('ul');
    var wrap; // used as outer-wrap object

    for(i=0;i<UL_array.length;i++) { // traverse UL elements

    elementArray = UL_array[i].getElementsByTagName('li');

    if(elementArray) {

    for(j=0;j<elementArray.length;j++) {

    wrap = document.createElement('div');
    wrap.style.backgroundColor='yellow'; // I have added this to
    highlight the swapped elements, will be removed when script works
    wrap.appendChild(elementArray[j]);

    UL_array[i].appendChild(wrap); // append wrapped div with
    internal li element to UL
    UL_array[i].removeChild(elementArray[j]); // remove existing LI

    } // end inner for

    } // end if

    } // end outer for

    } // end function


    ta
    brendan

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    when removing the childs of a parent using a loop, you must use a backwards decremental loop, because with every removment, the length of the collection decreases.

    On the other hand, you may use another aproach. have you considered replaceChild() method?
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

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