Home > Software engineering >  how to get one class from multiple classes in javascript
how to get one class from multiple classes in javascript

Time:12-06

let check1 = document.querySelectorAll('.container input');
        check1.forEach((elem)=>{
            elem.addEventListener('click',()=>{
                let boxes = document.querySelectorAll('.boxes');
                boxes.forEach((ele)=>{
                    let boxesId = ele.getAttribute("class");
                    console.log(boxesId) //now there are three classes shown (boxes karat size)how to get size class
                })               
            })
        })

i am trying to get one class from multiple classes in javascript

CodePudding user response:

Instead of using ele.getAttribute("class");

Use the classList property.

let boxesId = ele.classList[2];

classList will return a list of all the classes. See documantation

CodePudding user response:

Why you wanna do this? Sorry i can't understand. But if you wann check if a element have a clas you can use the method includes.

element.classList.contains(className)
  • Related