Search code examples
attributesclonejquery

jQuery each() to return false and start up again


I am having problems with the each() function. What I like to is to return false; and then start up again. You'll see the each() function is repeating only the first link. I would like the each link to clone each $('.ms-PostTitle a') ... See my code. Please Help!

========HTML======= What the each() function is currently doing :( ....

<div class="ms-PostTitle"> 
    <a href="Post.aspx?ID=1">Post #1</a>
</div>
<a href="Post.aspx?ID=1" class="read-more">Read More</a>

<div class="ms-PostTitle"> 
    <a href="Post.aspx?ID=2">Post #2</a>
</div>
<a href="Post.aspx?ID=1" class="read-more">Read More</a>

========HTML======= What I like it tooo doo....

<div class="ms-PostTitle"> 
    <a href="Post.aspx?ID=1">Post #1</a>
</div>
<a href="Post.aspx?ID=1" class="read-more">Read More</a>

<div class="ms-PostTitle"> 
    <a href="Post.aspx?ID=2">Post #2</a>
</div>
<a href="Post.aspx?ID=2" class="read-more">Read More</a>

========jQuery=======

    $('.ms-PostTitle a').each(function () {
       var href = $(this).attr('href');         
       $('<a class="read-more" href=' + href + '>Read More</a>').insertAfter('div.ms-PostBody p');
       return false;
});

Solution

  • This should work

    $('.ms-PostTitle a').each(function () { 
       $('<a>', { text: "Read More", class: "read-more", href: this.href }).insertAfter($(this).parent());
    });
    

    http://jsfiddle.net/RLa5C/1/