I have multiple same containers and each one contains same anchors like:
<div>
<a href="#" class="button1">Button 1a</a>
<a href="#" class="button2">Button 2a</a>
<div>
<div>
<a href="#" class="button1">Button 1b</a>
<a href="#" class="button2">Button 2b</a>
<div>
<div>
<a href="#" class="button1">Button 1c</a>
<a href="#" class="button2">Button 2c</a>
<div>
What I try to achieve is when I click on .button1 should start trigger and auto click on closest .button2
My js code now looks like:
const btn = document.querySelectorAll('.button1')
btn.forEach(function(btn) {
btn.addEventListener("click", function() {
console.log(this.innerText)
this.closest('.button2').click()
});
});
And here is fiddle: https://jsfiddle.net/4nhvtao1/4/