Question

There is a list as below.

1
2
3
4
5
<ul>
<li>A</li>
<li>B</li>
<li>C</li>
</ul>

If I want to move A or B or C to the first place after clicking on it, what should I do?

Answer

By using JQuery method prepend to make it happen.

Solution:

1
2
3
4
5
6
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
<li>A</li>
<li>B</li>
<li>C</li>
</ul>

1
2
3
$('ul li').click(function(event) {
$('ul').prepend($(this))
});

Reference


This is the end of post