1

When I run:

javascript:

var inputs = document.getElementsByClassName('subscribe_follow'); 

for(var i=0;i<inputs.length;i++) { 
   inputs[i].click(); 
}

to click all buttons with the class class="subscribe_follow", it also clicks all buttons with the class class="subscribe_follow ng-hide".

How do I only click the exact class?

Sushanth --
  • 55,259
  • 9
  • 66
  • 105

2 Answers2

5

You can use the not operator.

document.querySelectorAll('.subscribe_follow:not(.ng-hide)');

.subscribe_follow:not(.ng-hide) selector selects all the elements with the class subscribe_fellow but which do not have the ng-hide class.

jQuery

$('.subscribe_follow:not(.ng-hide)');
Sushanth --
  • 55,259
  • 9
  • 66
  • 105
1

You could use the not operator as suggested by Sushanth. The disadvantage here is that you'd have to mention every class that is supposed to be excluded, which may be a pain depending on the number of different classes that you might be using on the same elements.

I recommend declaring classes with the sole purpose of applying js, or targeting the elements by a different attribute that you could set in your HTML.

Here you can find more about alternative targeting:

Find an element in DOM based on an attribute value

barbarossa
  • 129
  • 1
  • 11