Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

select element and make it first element

<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>
like image 742
Yogesh Avatar asked Jul 22 '26 05:07

Yogesh


2 Answers

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.

like image 59
Dave L Avatar answered Jul 24 '26 19:07

Dave L


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>
like image 36
Hikmat Sijapati Avatar answered Jul 24 '26 17:07

Hikmat Sijapati



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!