Delete last class

When I click on a div, I want to remove the last of its classes (which is always the third). Thus, when I click on the div below, I want to delete class 3, which may have different class names (but always ends with "_hover").

Is there an easy way to do this?

<div id="container" class="class1 class2 class3"> $('#container').on('click', function() { $(this).removeClass(?); } 
+4
source share
1 answer

One easy way is to find the last class and then remove it:

 var lastClass = $('#container').attr('class').split(' ').pop(); $(this).removeClass(lastClass); 
+6
source

Source: https://habr.com/ru/post/1501558/


All Articles