开发者

Can't move li elements from one unorderted list to another

I have two un-ordered list

<ul id="#list1">
<li>one&l开发者_开发技巧t;/li>
<li>two</li>
</ul>

<ul id="#list2"></ul>

and two buttons

<input id="add" name="yt1" type="button" value="<<" /><br />
<input id="remove" name="yt2" type="button" value=">>" /> 

If the button with the id add is pressed all elements from #list1 should be move to #list2. How do I move elements from one list to another using JQuery

I though of something like the below, but not sure how to do the actual moving

$("#add").click(function(){
$("#list1 li").each(function(){
//Do not know what to put in here
}
})


You can use appendTo:

$("#add").click(function(){
    $("#list1 li").appendTo('#list2');
});

DEMO

Also change your IDs from <ul id="#list1"> to <ul id="list1">.


  //this will move selected items from yt1-list to yt2-list     
  $("#yt1 option:selected").appendTo("#yt2");

  //this will move all selected items from yt1-list to yt2-list
  $("#yt1 option").appendTo("#yt2");


      $('#list2').html( $('#list1').html() );


Try this. Didn't test it, but it should work (logically).

$("#add").click(function(){
$("#list1 li").each(function(){
var holder;
$(this) = holder;
$(this).remove();
$('#list2').append();
}
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜