jquery clone only once

Posted by tsiger on Stack Overflow See other posts from Stack Overflow or by tsiger
Published on 2010-04-25T12:18:52Z Indexed on 2010/04/25 12:23 UTC
Read the original article Hit count: 300

Filed under:
|
|

I have a series of divs like this:

<div id="available-posts">
    <div class="post" id="unique_id">
      <a href="#" class="addme">Add</a> Some text
    </div>

    <div class="post" id="unique_id">
      <a href="#" class="addme">Add</a> Some text
    </div>
</div>

The unique_id is a different number for each of the divs.

Now i have a new empty div like this

<div class="related-posts"></div>

And i clone items in there.

The thing is that i want to check if an item is already cloned. If it is i want to stop it from being cloned again.

This is how i clone items:

    // clone it
$('.addme').live('click', function() {
        $(this).parents('div.thepost').clone().fadeIn('normal').appendTo('#related-posts').find('a.addme').html('Remove').removeClass('addme').addClass('removeme');
        return false;
}); 

// remove it
$('.removeme').live('click', function() {
        $(this).parents('div.thepost').fadeOut('normal', function() {$(this).remove(); });
        return false;
}); 

In other words i want the cloned list to contain only unique items. Not for example 2 clones of the same post.

*edit: i am using live coz the first list (available posts) is populated through an AJAX call.

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about clone