Link to home
Create AccountLog in
Avatar of Jamie
JamieFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Open grouped links on a page in new tabs via jquery

Hello Experts,

If I have a number of grouped links on a page - how can I automatically open then into new tabs by a press of a button.

e.g.

<script>
$('#ButtonOne').click(function() {
    $('a').each(function() {
        window.open($(this).attr('href'));
    });
});
</script>

<a id="group1" href="page1.html">Click Here</a><br />
<a id="group1" href="page2.html">Click Here</a><br />
<a id="group1" href="page3.html">Click Here</a><br />
<a id="group2" href="page4.html">Click Here</a><br />

<button type="button" id="group1">Open Links</button> - should only open page1.html, page2.html, page3.html
<button type="button" id="group2">Open Links</button> - should only open page4.html

Many thanks

Jamie
Avatar of Mukesh Yadav
Mukesh Yadav
Flag of India image

Try this ;)

<script>
$('.groups').click(function() {
    $('a' + '.' + $(this).attr('id')).each(function() {
        window.open($(this).attr('href'));
    });
});
</script>

<a class="group1" href="page1.html">Click Here</a><br />
<a class="group1" href="page2.html">Click Here</a><br />
<a class="group1" href="page3.html">Click Here</a><br />
<a class="group2" href="page4.html">Click Here</a><br />

<button type="button" id="group1" class="groups">Open Links</button> - <!-- should only open page1.html, page2.html, page3.html  -->
<button type="button" id="group2" class="groups">Open Links</button> - <!-- should only open page4.html  -->

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of Jamie

ASKER

Hi Mukesh,

Many thanks for your reply, but unfortunately, for some reason, I could not get it to work.

Regards

Jamie
Avatar of Jamie

ASKER

Hi Michael,

Many thanks for your reply and solution, it works perfectly :)

Very much appreciated.

Regards

Jamie
You are welcome :)