Reputation: 1184
Can anyone tell me of a way to open all links within an id in a new window?
Upvotes: 1
Views: 1487
Reputation: 10329
Try this:
$('#myId').click(function(){
this.target = "_blank";
});
Upvotes: 0
Reputation: 2560
I'd do it like this:
$('#myId a').click(function() {
window.open(this.href);
return false;
});
Upvotes: 2
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
Reputation: 2354
Put this in the head:
$(function () {
$('#selector').attr('target', '_blank');
})
Upvotes: 2
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