jQuery addClass example

Change class name on click in jQuery

by Mukesh Singh Negi

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<div>
  <ul>
    <li>Example 1</li>
    <li>Example 2</li>
    <li>Example 3</li>
  </ul>
</div>

<h4></h4>

CSS

li {
  list-style: none;
}

JavaScript

$('li').each(function(idx){
      $(this).attr("value", idx);
    })
    
    const getValue = $('li:first-child').attr("value");
    $('h4').text(getValue);
    
    $('ul').sortable({
      update: function() {
        var _li= $('li');
        _li.removeAttr("value");
        _li.each(function(idx) {
          var currentObj=$(this);
          console.log(currentObj.text());
          $(this).attr("value", idx);
        })
      }
    });