The problem is that querySelectorAll
returns an array-like-object and it does not have a click()
function. The items that are members of this array-like-object are elements and they all have individually a click
function, but you cannot invoke it from the array-like-object (a NodeList, to be precise) that contains them.
In the accepted answer of @mplungjan, we see this approach
photo.querySelectorAll('div[role="checkbox"]').forEach(div => div.click());
which is correct.
But, you can also create a click
function for a NodeList
and then you would be able to run your initial code :)
NodeList.prototype.click = function() {
for (let item of this) item.click();
};
document.querySelectorAll(".hd").forEach(item => item.parentNode.querySelectorAll("[type=button]").click());
<div>
<input type="button" value="1" onclick="console.log(1)">
<input type="button" value="2" onclick="console.log(2)">
<input type="button" value="3" onclick="console.log(3)">
<input type="hidden" class="hd">
</div>
<div>
<input type="button" value="4" onclick="console.log(4)">
<input type="button" value="5" onclick="console.log(5)">
<input type="button" value="6" onclick="console.log(6)">
</div>
<div>
<input type="button" value="7" onclick="console.log(7)">
<input type="button" value="8" onclick="console.log(8)">
<input type="button" value="9" onclick="console.log(9)">
<input type="hidden" class="hd">
</div>
As we can see, once we define a click
function for a NodeList that clicks all the elements inside, we can reuse it as many times as we would like.
So, while mplungjan's answer is correct and deserves to be accepted, I have decided to write a new answer about how we can add the feature that you missed here rather than working around it.