Szmerd Szmerd - 5 months ago 7
Javascript Question

Click on element and move to first place in the list

I'm haveing problem to complete the click handler,which moves the clicked element to the first place in the list. And if I click B list should be reordered to B A C

eg. list

<ul>
<li>one</li>
<li>two</li>
<li>three</li>
</ul>


And i started with

function regh() {
$('ul li').click(function (event) {

});
}

Answer

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

Use .prepend()

Description: Insert content, specified by the parameter, to the beginning of each element in the set of matched elements.