<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
<li>Four</li>
</ul>
when I click on three I want it to be like:-
<ul>
<li>Three</li>
<li>One</li>
<li>Two</li>
<li>Four</li>
</ul>
If you have jQuery you can use:
$('#items li').click(function() {
$(this).parent().prepend(this);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<ul id = "items">
<li>One</li>
<li>Two</li>
<li>Three</li>
<li>Four</li>
</ul>
I can also write a non-jQuery one up if needed.
handle click event with use of remove() and prepend() method.
$("body").on("click",'li',function(){
$('ul').prepend($(this));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<ul>
<li>one</li>
<li>two</li>
<li>three</li>
<li>four</li>
</ul>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With