Remove LI using jQuery

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ziycon
    Contributor
    • Sep 2008
    • 384

    Remove LI using jQuery

    I have the code below, I need to remove the opening UL and LI and the closing LI and UL and just keep the inner UL.

    I've tried the everything I can think of but not being an expert in jQuery is letting me down, any help much appreciated.
    Code:
    <ul>
      <li>
        <ul>
          <li>item 1</li>
          <li>item 2</li>
          <li>item 3</li>
        </ul>
      </li>
    </ul>
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    what you would need to do is (at laest when using DOM)
    - copy the inner <ul>
    - replace the outer <ul> with the copied node

    although don’t ask me how to do that with jQuery …

    Comment

    • ziycon
      Contributor
      • Sep 2008
      • 384

      #3
      Well if you have any other ideas apart from jQuery by all means do tell.

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        assuming there are ids I can hook on (that would be easier in jQuery)
        Code:
        <ul id="outer">
          <li>
            <ul id="inner">
              <li>item 1</li>
              <li>item 2</li>
              <li>item 3</li>
            </ul>
          </li>
        </ul>
        Code:
        var iul = document.getElementById("inner").cloneNode(true);
        var oul = document.getElementById("outer");
        oul.parentNode.replaceChild(iul, oul);
        jQuery would take care of cross browser issues …

        Comment

        • ziycon
          Contributor
          • Sep 2008
          • 384

          #5
          Is it possible to do it like below as there are no id or classes?

          Or something similar like that?
          Code:
          var iul = document.getElementById("div#list ul li ul").cloneNode(true);
          var oul = document.getElementById("div#list ul");
          oul.parentNode.replaceChild(iul, oul);

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            Is it possible to do it like below as there are no id or classes?
            not without jQuery. in pure DOM that doesn’t work. maybe when you check out the jQuery API, you find a method, that does the replace.

            Comment

            • ziycon
              Contributor
              • Sep 2008
              • 384

              #7
              I'm 98% there, last problem is I can't add the LI to the beginning of the UL, the append keeps adding it to the bottom, I've tried using the :first but doesn't seem to work, any ideas?
              Code:
              <script type="text/javascript">
              $(document).ready(function() {
                $("div#section_Left_Panel ul").attr("id","outer");
                $("div#section_Left_Panel ul li ul").attr("id","inner");
                var iul = document.getElementById("inner").cloneNode(true);
                var oul = document.getElementById("outer");
                oul.parentNode.replaceChild(iul, oul);
                $("div#section_Left_Panel ul").attr("id","menu");
                $("div#section_Left_Panel ul").append("<li><span class=\"Current\"><a class=\"link_Home\" title=\"Home\" href=\"/\">Link</a></span></li>");
              });
              </script>

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                last problem is I can't add the LI to the beginning of the UL, the append keeps adding it to the bottom
                well, that’s what append() usually does … there might be a jQuery method that works like DOM’s Node.insertBefo re()
                Last edited by Dormilich; Oct 5 '10, 02:18 PM.

                Comment

                • ziycon
                  Contributor
                  • Sep 2008
                  • 384

                  #9
                  Found it, .prepend() Thanks for all your help with this.

                  Comment

                  • kovik
                    Recognized Expert Top Contributor
                    • Jun 2007
                    • 1044

                    #10
                    Just to note, using the innerHTML property ($(node).html() , in jQuery) is much faster to perform than DOM manipulation. You could just take the innerHTML of the inner list and replace it into the innerHTML of the outer list.

                    Code:
                    var parentList = $("#target-list");
                    var childList = parentList.find("ul");
                    
                    if (childList.length > 0) {
                        parentList.html(childList.first().html());
                    }
                    And as a side note, if you are using IDs, there's no need to specify the element type in the selector. You're only slowing jQuery down.

                    Comment

                    • ziycon
                      Contributor
                      • Sep 2008
                      • 384

                      #11
                      Hi Kovik, thanks for your input, could you explain that a bit more please, I'm not sure I 100% understand how your codes meant to work?

                      Comment

                      • kovik
                        Recognized Expert Top Contributor
                        • Jun 2007
                        • 1044

                        #12
                        Basically, parentList is the outer list that you are looking for (for you, it's "#section_Left_ Panel ul"). childList is the <ul> element inside of the target list. What this code does is finds both of these elements. The if-statement checks if the inner list actually exists and, if it does, it takes the HTML from the inner list, which is all of the <li> elements, and replaces the outer list's HTML with it.

                        So, you go from this:
                        Code:
                        <ul id="target-list">
                            <li>
                                <ul>
                                    <li>foo</li>
                                    <li>bar</li>
                                </ul>
                            </li>
                        </ul>
                        To this:
                        Code:
                        <ul id="target-list">
                            <li>foo</li>
                            <li>bar</li>
                        </ul>

                        Comment

                        • ziycon
                          Contributor
                          • Sep 2008
                          • 384

                          #13
                          Nice one kovik, I understand now, thanks for the tip.

                          Comment

                          Working...