views:

29

answers:

2

I want to append the content within my 'li' to the 'trashbin' div when the link class 'ui-icon-trash' is clicked. I have a list and some li content like so:

  <li class="ui-widget-content ui-corner-tr">
                    <h5 class="ui-widget-header">item1</h5>
                    <img src="tiem.png"  alt="item1"/>
                    <div class="draggableicons">
                    <a style="float:left" href="#" title="product information here" class="ui-icon ui-icon-circle-plus">More info...</a>
<a href="link/to/cart/script/when/we/have/js/off" title="Delete this image" 
class="ui-icon ui-icon-trash">
Delete image</a>
                    </div>
                </li>

    <div id="trashbin"></div>

I want to append everything within the 'li' tag to a 'div' when 'ui-icon-trash' is clicked. I want it to be cloned then appended so that I can run it multiple times. How can i do this????

+1  A: 

This should do the trick:

$(function(){
  $('a.ui-icon-trash').click(function(){
    $(this).closest('li').appendTo('#trashbin ul');
    // or if you want to keep the original:
    $(this).closest('li').clone().appendTo('#trashbin ul');
  });
});

You need to add an <ul/> element to the trashbin, because an <li/> must be encapsulated in it.

jigfox
$(function(){ $('a.ui-icon-trash').click(function(){ $(this).closest('li').clone().appendTo('#trashbin ul'); });});Forgot the clone. Yep working
I didn't forget it. Since you wanted to append to to a trashbin, I thought you want it to be removed from the original list. Because that is how a trashbin works in most Desktop OS
jigfox
In his defense (for better or worse) the question did ask *"I want it to be cloned then appended so that I can run it multiple times. How can i do this????"* (and doesn't look like it's been edited).
R0MANARMY
A: 

When javascript is on you will want to add an onclick event to the delete this image link. I use the onclick method rather than depending on jquery to add it because it is easier to control server side. If you would rather just move the list item to the trash rather than add a copy just remove the .clone() from the jquery in the sendListItemToTrash function.

<script>
function sendListItemToTrash(ListItem)
{
    $(ListItem).clone().appendTo('#trashbin ul');
}
</script>
  <li class="ui-widget-content ui-corner-tr">
                    <h5 class="ui-widget-header">item1</h5>
                    <img src="tiem.png"  alt="item1"/>
                    <div class="draggableicons">
                    <a style="float:left" href="#" title="product information here" class="ui-icon ui-icon-circle-plus">More info...</a>
<a href="link/to/cart/script/when/we/have/js/off" onclick="javascript:sendListItemToTrash($(this).closest("li"));" title="Delete this image" 
class="ui-icon ui-icon-trash">
Delete image</a>
                    </div>
                </li>

    <div id="trashbin"><ul></ul></div>
Jonathan Park
This is not very unobtrusive. Why should it be better controlled on the server side if I use the `onclick` attribute? I believe it is better to pack all JavaScript together. So I can change everything which has anything to do with JavaScript in one place. Instead of switching between all files too find every thing where I used a javascript function that may be changed.
jigfox
I honestly prefer this method not from an efficiency of updates standpoint but from a efficiency of page load. In highly complex situations the jquery way of applying the click events to all the items ends up slowing page load in IE and you start having to think about what happens if someone interacts before the javascript finishes applying.
Jonathan Park