A click on the first link triggers a click on the rest

Viewed 60

Please tell me how to do it correctly, if a click was made on the first link, then to simulate a click on the rest?

jQuery('a.one_num').click(function() {
  jQuery('a.two_num').click();
  jQuery('a.three_num').click();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a href="#" class="one_num" type="number">test1</a>

<a href="https://yandex.com/" target="blank" class="two_num" type="number">test2</a>

<a href="https://www.google.com/" target="blank" class="three_num" type="number">test3</a>

2 Answers

If your goal is just click on the other elements, then, first of all, your links should have a common selector. For example, give a common class name to them. Then you can do something like:

document.querySelector('a.one_num')
.addEventListener('click', ev => {
  document.querySelectorAll('a[type=number]')
  .forEach(a => ev.target !== a && a.click())
})

But if you are trying to actually open a bunch of new tabs in the user's browser, one for each of your links, then I think it won't work, because modern browsers will likely block that attempts.

try window.open(otherlinkurl, windowName, [windowFeatures]); for the other links when one of them is pressed

it would be probably like:

window.open(firsturl);

window.open(secondurl);

window.open(thirdurl);
Related