views:

3237

answers:

3

I'm trying to clone a list item, and then append it to the bottom of that cloned list item, note that it should be below the list item being cloned, not at the bottom of the list as I can do that myself. The purpose is to use it with jQuery.ui sortable.

Everything is working fine, in fact I can even get the cloning and the appending right. However, it appends it before the closing </li> tag, and for the life of me I can't force it to append after this tag.

This is the HTML Markup:

<ul id="column-2" class="connectedSortable">
    <li class="ui-state-default">
     <div class="label">feature</div>
     <div class="action">
      <div class="delete"></div>
      <div class="other"></div>
     </div>
    </li>
</ul>

The part we're concerned with is class="other" which upon being clicked on will duplicate the list item.

This is the jQuery so far:

// This works fine
$(".other").click(function() {

    // This needs to be set (global) to be used later on in some future code.
    actionTarget = this;   

    // This grabs the whole list item
    var targetStory = $(actionTarget).parent().parent();

    // This clones the list item, as well as appending 
    // that clone to the cloned list item
    $(targetStory).clone().appendTo(targetStory);

})

This works well, it grabs the list item, clones it, then dumps it on the screen - but in the wrong place :(

<ul id="column-2" class="connectedSortable">
    <li class="ui-state-default">
     <div class="label">feature</div>
     <div class="action">
      <div class="delete"></div>
      <div class="other"></div>
     </div>
     <li class="ui-state-default">
         <div class="label">feature</div>
         <div class="action">
          <div class="delete"></div>
          <div class="other"></div>
         </div>
     </li>
    </li>
</ul>

Anyone have any idea why it's not appending to the end of the list item being cloned, and how to resolve this?

A: 

i think what you want is this:

$(targetStory).clone().appendTo(targetStory.parent());
pixeline
Almost, it's something Id' already tried. Instead, it adds it to the bottom of the list, rather than after the list item being cloned.
jakeisonline
+3  A: 

perhaps you want to use after instead of appendTo

cobbal
Yep! This does the trick, thanks cobbal$(targetStory).after(targetStory.clone());
jakeisonline
For reference:http://docs.jquery.com/Manipulation/appendTo States that the element you have , it will be inside it - just placed at the end of the content inside it. Hence the word, Appending. .After like you found is the way to go.
SuperRoach
True, I missed the clarification of "inside it", too much skimming.
jakeisonline
A: 

I think insertAfter is what you're looking for. after would insert the original node after the clone, rather than the clone after the original node. Something like this should work:

$(targetStory).clone().inserAfter(targetStory);
mishac
Thanks Mishac, cobbal already managed to nab the answer! $(targetStory).after(targetStory.clone());
jakeisonline