/ JAVASCRIPT, FRONT-END

How to click on the element and move to the first in javascript?

Question

There is a list as below.

<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:

<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>
$('ul li').click(function(event) {
  $('ul').prepend($(this))
});

Reference


This is the end of post