JQuery - A Practical Guide. Duplicate the <li> and put it at the end of the <ul>

I have the usual ul li. I want to bind li to a list that is based on whether it is first on the list.

HTML

<div id="gallery"> <ul> <li>Point Nr 1<p>lol</p></li> <li>Point Nr 2</li> <li>Point Nr 3</li> <li>Point Nr 4</li> </ul> </div> 

pseudo javascript / jquery

 $("#gallery ul li").first().duplicate(attachTo:"#gallery ul li") 

It looks like this:

 <div id="gallery"> <ul> <li>Point Nr 1<p>lol</p></li> <li>Point Nr 2</li> <li>Point Nr 3</li> <li>Point Nr 4</li> <li>Point Nr 1<p>lol</p></li> </ul> </div 

How can I do it?:)

+4
source share
2 answers

I think it should be

  var ul = $ ('# gallery ul');
 ul.find ('li: first'). clone (true) .appendTo (ul);
+11
source

Must be:

 var $gallery = $('#gallery'); $gallery.find('li:first').clone(true).appendTo($gallery.find('ul')); 

Example: http://www.jsfiddle.net/4yUqL/22/

Ref . : . clone ()

+2
source

Source: https://habr.com/ru/post/1332729/


All Articles