Reputation: 2019
I have multiple anchor tag in a page. I want to click all the tag from jQuery to open each link in a new tab. But it is working only for the first element.
My attempt is below:
$('.tbl a').each(function () {
var url = $(this).attr("href");
window.open(url, '_blank');
})
Note: if I set background color in each it works well. Then why not new Tab?
Upvotes: 0
Views: 428
Reputation: 81
Building on the answer in the comments,
$('.tbl a').each(function () {
$(this).on("click", function(e){
window.open(e.target.href, '_blank');
})
})
Upvotes: 0
Reputation: 2492
My suggestion from the comments would look something like this :
Won't work here, because the sandoxed frame does not allow popups, but you get the idea.
$('.opener').on('click',function(){
$('ul a').each(function (index) {
var url = $(this).attr("href");
window.open(url, '_blank'+index);
})
})
.opener{
display:inline-block;
background-color:#ccc;
cursor:pointer;
color:#FFF;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h1>open new tabs</h1>
<p>An unordered list:</p>
<ul>
<li><a href="https://www.coffe.com">Coffee</a></li>
<li><a href="https://www.tea.com">Tea</a></li>
<li><a href="https://www.milk.com">Milk</a></li>
</ul>
<p class="opener">test open </p>
Upvotes: 1