user1687891
user1687891

Reputation: 864

How to get all the classname used inside the button from one class?

I have a button with multiple classname a, b and active.

<button class="a b active"></button>

I want to get the classname a & b only using active. I have used following code

document.querySelectorAll('.active')

I get

<button_ngcontent-c1="" class="a b active" ng-reflect-klass="0,0" ng-reflect-ng-class="[object Object]"></button>

I am not being able to get this class a and b.

Can anybody help?

Upvotes: 0

Views: 58

Answers (1)

user4676340
user4676340

Reputation:

querySelectorAll gives you a NodeList.

From that, spread that list into an array, and iterate over it to get the className property :

const nodeList = document.querySelectorAll('.active');

const els = [...nodeList];

for (let el of els) {
  console.log(el.className.split(' '));
}
<button class="a b active"></button>
<button class="c d active"></button>
<button class="e f active"></button>

Upvotes: 1

Related Questions