Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery onClick Move this element to top (as first Child of UL)

Tags:

jquery

OnClicking on any <li><a>....</a></li> element except last child, this element should be moved to top as first child of <ul>.


FIDDLE


Description

By Default:

<ul>
    <li><a href="#">01</a></li>
    <li><a href="#">02</a></li>
    <li><a href="#">03</a></li>
    <li><a href="#">04</a></li>
    <li><a href="#">05</a></li>
    <li><a href="#">06 (Last Child)</a></li>
</ul>

If I click on 03, this list should become as below..

<ul>
    <li><a href="#">03</a></li>
    <li><a href="#">01</a></li>
    <li><a href="#">02</a></li>
    <li><a href="#">04</a></li>
    <li><a href="#">05</a></li>
    <li><a href="#">06 (Last Child)</a></li>
</ul>

But If Click on last child <li> this element should not be moved or changed... last child should remain as last always.

like image 586
Reddy Avatar asked May 19 '15 07:05

Reddy


People also ask

How to append an element as a first child in jQuery?

The . prepend() method inserts the specified content as the first child of each element in the jQuery collection (To insert it as the last child, use . append() ).

Can you use first child with jQuery?

The jQuery:first-child selector is used to selects all elements that are the first child of its parent. The jQuery:first-child selector is a built-in selector which is used on the HTML element. The first-child selector is similar to :nth-child(1), while the . first() method selects only single element.

How do you create a new element as a first child to the parent element?

To insert element as a first child using jQuery, use the prepend() method. The prepend( content ) method prepends content to the inside of every matched element.

How to prepend an element in jQuery?

The prepend() method inserts specified content at the beginning of the selected elements. Tip: To insert content at the end of the selected elements, use the append() method.


3 Answers

You can have a click event for li element except last element using $('li:not(:last)') element selector. and then use prependTo for appending as first child to parent:

$('li:not(:last)').click(function(){
 $(this).prependTo($(this).parent());
});

Working Demo

Update: Solution for targetting multiple UL elments

In case for multiple UL elements, you need to use :last-child instead of :last.As :last targets last element in matched set whereas :last-child will target all li that are last child.

like image 129
Milind Anantwar Avatar answered Oct 28 '22 16:10

Milind Anantwar


You can use :not(:last) filter and jQuery prependTo:

$('li a:not(:last)').click(function() {

    $li = $(this).parent('li');

    $li.prependTo($li.parent('ul'));

    return false;
})
like image 31
Jake Opena Avatar answered Oct 28 '22 16:10

Jake Opena


Check this updated JSFiddle

This works as per suggested. Just add the jquery to your code

like image 29
Amar Kamthe Avatar answered Oct 28 '22 15:10

Amar Kamthe