jay stephens jay stephens - 2 months ago 9
HTML Question

How to specifically .remove a class that had an added class in jquery

I have a few buttons that remove aspects of a class list, each class list has the same name but have an added class to specify what category they are e.g.

<li class="alerts news"> </li>
<li class="alerts weather"> </li>
<li class="alerts sports"> </li>


What I'm try to do is when a specific button that removes, say the 'Alert news' and without removing the rest of the alerts, how can you explain to jquery that you want just that element with that added class to be .removed.

jquery -

$(document).ready(function () {
$(".button").click(function () {
if($(".toggle").hasClass("toggleOff")) {

$(".alerts").hasClass('News').remove();

}
});
});


The hasClass doesn't seem to work, can't seem to remove any specific alerts without removing them all. Any help would be appreciated.

Answer

hasClass() returns a boolean, it isn't used as a selector method

Change

$(".alerts").hasClass('News').remove();

To

$(".alerts.news").remove();

This will remove only the alerts class that also have the news class

Comments