I'm trying to get the class name of a child element using JS. I've tried the following, but it won't work. Any help is much appreciated!
var selectedicon = document.getElementByClassName('selected-icon');
var iconclass = selectedicon.getElementsByTag("i").className;
It sounds like you want the
class of the first
i inside the first
selected-icon element. If so:
var i = document.querySelector(".selected-icon i"); console.log(i ? i.className : "Not found");
<div class="selected-icon"> <i class="hummma"></i> </div>
document.querySelector accepts a CSS selector and finds the first element in the DOM that matches it. There's also
document.querySelectorAll which returns a list of matching elements. There are versions of both of them on elements as well, which only look within that element's descendants.