I am using getElementsByClassName to select a single div by it's unique class name. For some reason, using querySelector is not working to select this div by it's unique class name, it returns "null."
I am trying to make this single div either display:none, add/remove a class, or just not be visible, I don't really care at this point. It's returning an HTML Collection, with the correct single div in it so length is 1, the div I want is at position 0. Every time I try to access this div from the collection, I'm getting null.
So this code:
let eleList = document.getElementsByClassName("CustomFormFieldQuestion_252145870712756");
console.log(eleList);
console.log(eleList.item(0));
Is returning to me this in the console:
HTMLCollection { length: 0 }
0: <div class="at-row at-row-full Custo…uestion_252145870712756">
length: 1
<prototype>: HTMLCollectionPrototype { item: item(), namedItem: namedItem(), length: Getter, … }
null
The div is it grabbing is the correct div; however, when I try to access it to either add a class or give it style.display = "none" all I get in the console is "null."
I have a list of about 10 items I need to grab by their unique class, hide them when the page initializes, then make them visible based on a checkbox response in a previous question.
I tried converting the HTML Collection to an Array and access it that way,
let arrayEleList = Array.from(eleList);
And it returned an empty array.
What am I doing wrong here, the initial eleList returns the correct div, but whenever I try to access it to do something I get "null."
This code uses document.querySelector to select the first element with the specified class name. It then checks if the element exists, and if it does, you can proceed to manipulate it by changing its style.display or adding a class.