By
05/13/2016   Comments Off on Add a class to the currently clicked element in a parent

View all snippets

The following code will add a class called active to the list element that is currently clicked.

jQuery(function () {
    jQuery(".remove-bullets li").click(function (e) {
       e.preventDefault();
       jQuery(".remove-bullets li").addClass("active").not(this).removeClass("active");
    });
});

The HTML

<ul class="remove-bullets">
   <li>...</li>
   <li>...</li>
</ul>

On the click of one of the list elements, a class would be added like so:

<ul class="remove-bullets">
   <li>...</li>
   <li class="active">...</li>
</ul>