Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove last class [duplicate]

When clicking on a div I want to remove the last of it's classes (which is always the third). So, when clicking on the div below I want class3 to be removed, which can have different class names (but always ends with '_hover').

Is there an easy way of doing this?

<div id="container" class="class1 class2 class3">

$('#container').on('click', function() {
    $(this).removeClass(?);
}
like image 909
holyredbeard Avatar asked Sep 10 '13 21:09

holyredbeard


Video Answer


1 Answers

One simple way would be to find the last class, then remove it:

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

In vanilla JavaScript, you can use:

class.classList.add('className');

// or
 
class.classList.remove('className');
like image 135
AliBZ Avatar answered Sep 19 '22 13:09

AliBZ