I want to get the ID of an element that I found by using getElementsByClassName()
Here is what my code looks like:
let className = document.getElementsByClassName('some-class')
for (let i = 0; i < className.length; i++) {
if (className.GETID.toLowerCase().includes('some text')) {
// do something
}
}
So basically what I want is to replace GETID with some kind of method or anyway to get the id of the Element that I have found by using the getElementsByClassName() method.
>Solution :
See Element.id. Note that this is not a method as your question asks for, but a property instead. This is how you’d use it with your code:
let className = document.getElementsByClassName('some-class')
for (let i = 0; i < className.length; i++) {
if (className[i].id.toLowerCase().includes('some text')) {
// do something
}
}
The .id property is set for every DOM element regardless of whether it was retrieved with getElementById().