Question: I am using several anchors and when clicking on an anchor, the class switches to **img-swap on**
If I click on another anchor, the class of that anchor switches also on **on**
What I want to achieve: if 1 anchor has already the class on **on**, all other anchors must be blocked to switch on **on**

```
jQuery(document).ready(function ($) {
 $('a.img-swap').click(function () {
  $(this).toggleClass('on');
 }); 
});

```

Is this possible to do a check in javascript when a class is on **on** and if so, block the other anchors to switch from class?

Answer: 
I would just bind handler to be fired only once:

```
jQuery(document).ready(function ($) {
 $('a.img-swap').one('click', function () {
  $(this).toggleClass('on');
 }); 
});

```

Now if you want state `on` to be switchable, i'd use instead:

```
jQuery(document).ready(function ($) {
 $('a.img-swap').click(function () {
  if($('a.img-swap.on').not(this).length) return;
  $(this).toggleClass('on');
 }); 
});

```
Use [`hasClass`](https://api.jquery.com/hasclass/) to check if element has a class.

```
jQuery(document).ready(function ($) {
    $('a.img-swap').click(function () {
        if(!$("a.img-swap").hasClass("on"))
            $(this).toggleClass('on');
    });
});

```