maftyycs maftyycs - 1 year ago 59
Javascript Question

Give a class of "selected" when clicked on another <a> then remove the class if it's the same <a>

I am trying to change

$('h2 a').on('click', function(){
$('h2 a.selected').removeClass('selected');

I made an Accordion, the first tab has class
. So when I click on another tab, it removes
from the first tab and gives the one I clicked on
, if I click on the same tab I just opened, it removes

Answer Source

You can use .is(), .filter()

var h2 = $("h2 a");

h2.on("click", function() {
  if ($(this).is(".selected")) {
  } else {
.selected {
  color: red;
<script src="">

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download