Jackson
Jackson

Reputation: 1184

jQuery - Open all links in id in new window

Can anyone tell me of a way to open all links within an id in a new window?

Upvotes: 1

Views: 1487

Answers (6)

Paul Peelen
Paul Peelen

Reputation: 10329

Try this:

$('#myId').click(function(){
    this.target = "_blank";
});

Upvotes: 0

jammus
jammus

Reputation: 2560

I'd do it like this:

$('#myId a').click(function() {
    window.open(this.href);
    return false;
});

Upvotes: 2

sergionni
sergionni

Reputation: 13510

If you say "all links" ,that i understand that any of specific link (e.g. may be more than one on the page) should guide to blank page.

For this case, you may do all needed links,that guide to blank page ,with specific id and dynamically generated postfix, e.g.:

link_1, link_2 etc.

so the script will look like:

var linkId = "[id*=" + "link_]";
$(linkId).attr('target', '_blank');

here is regexp used.

Upvotes: 0

Nick Spiers
Nick Spiers

Reputation: 2354

Put this in the head:

$(function () {
    $('#selector').attr('target', '_blank');
})

Upvotes: 2

Jimmeh
Jimmeh

Reputation: 2862

$('#id a').click(function() {
    this.target = "_blank";    
}

Upvotes: 0

o.k.w
o.k.w

Reputation: 25790

Is it a single function that will open all the hyperlinks within an ID in new windows? That's what I thought. :P

$("#some_id a").each(function (i) {
    window.open(this.href);
  });

Upvotes: 1

Related Questions