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 Coder
    Join Date
    Mar 2012
    Posts
    81
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Simple jQuery, changing HTML

    I think I'm half way there. I need to change this HTML with jQuery:

    Code:
    <div class="main-class">
      <div class="div1">
      content 1
      </div>
      <div class="div2">
      content 2
      </div>
    </div>
    
    $("div.main-class").each(function() {
    
      var holder = $(this);
    
      $("> div", holder).each(function(){
    
      //here I need to get entire div, including its contents, and place it before the main-class (and remove the old HTML)
      //holder.before() ???
    
    }
    Which should leave this:
    Code:
    <div class="div1">
    content 1
    </div>
    <div class="div2">
    content 2
    </div>
    <div class="main-class">
    </div>

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,983
    Thanks
    56
    Thanked 557 Times in 554 Posts
    this seems to work...
    Code:
    <script>
    $("div.main-class>div").each(function() {
    $(this).insertBefore($(this).parent());
    });
    </script>


  •  

    Posting Permissions

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