EDIT with reproducible example, and following T.J. Crowder suggestion below (tnx)
I need a button to be automatically clicked when I click on an element with class 'menu'.
I can get the element on the DOM, length is ok, and when I put a debug watch on it2, all properties seem ok. However, in the application I'm applying it the click event is not fired. In that case the 'menu' items have also their own click event listeners doing their stuff, which might hold the reason. Anyone have an idea why may not be working?
I have a similar working example that I tried, and apparently works
document.querySelectorAll('.menu').forEach(item => {
item.addEventListener('click', event => {
if (document.querySelectorAll('.savebtn').length>0){
var all = document.querySelectorAll('.savebtn');
var it2=all[0];
it2.click();
}
})
})
<ul>
<li><a href="http://www.google.com/search?q=1" target="_blank" class="menu" > link 1</a></li>
<li><a href="http://www.google.com/search?q=2" target="_blank" class="menu"> link 2</a></li>
</ul>
<form action="http://www.google.com/search" target="_blank">
<input type="text" name ="q">
<input type="submit" class = "savebtn" value="Submit">
</form>
any help ? Thanks