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 4 of 4
  1. #1
    New Coder
    Join Date
    Mar 2012
    Posts
    81
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Simple jQuery code

    Why does this simple jQuery not work?

    http://page-test.co.uk/jq.html

    It should result in this:

    <div class="main">
    </div>
    <div>1</div>
    <div>2</div>
    <div>3</div>

  • #2
    Senior Coder DanInMa's Avatar
    Join Date
    Nov 2010
    Location
    Salem,Ma
    Posts
    1,577
    Thanks
    13
    Thanked 248 Times in 248 Posts
    Code:
    var stored_html = "";
    
    $(".main > div").each(function() {
    
    	$(this).insertAfter(stored_html);
    
    });
    
    $(stored_html).insertAfter($(this).parent())
    what do you intend for it to do exactly? the last line does nothing because you havent assigned it to an element, so using $(this).parent() does nothing, as it has no parent to reference.

    the first part, well all you are doing is inserting, literally, nothing, not event a blank space.

  • #3
    New Coder
    Join Date
    Mar 2012
    Posts
    81
    Thanks
    7
    Thanked 0 Times in 0 Posts
    I want to convert the HTML in the example to:

    <div class="main">
    </div>
    <div>1</div>
    <div>2</div>
    <div>3</div>

    ... using this:
    Code:
    $(".main > div").each(function() {
    ...to pass through each div.

    It's a small part of a larger script. I just pulled this out so as not to complicate things.

  • #4
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,883
    Thanks
    56
    Thanked 539 Times in 536 Posts
    how is this question different from this one?


  •  

    Posting Permissions

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