Reputation: 767
I have a javascript link I want to open, but it is only working the second I click on it. I want to click a Div I have created for this, and there are no anchor tags involved.
The HTML I use:
<li>
<div class="artikel_box_lk">
<div class="button" rel="$productNameSeo">
<span>More Info >></span>
</div>
</div>
</li>
The javascript code I use for this:
$(function(){
$(".artikel_box_lk").click(function(e) {
if($(e.target).hasClass("button")) window.open("/lk/" + $(e.target).attr("rel"), "_blank");
return false;
});
});
So, the strange thing happening is that if I click on it a couple of times it sometimes works. I must have forgotten something crucial here, but cannot seem to find out.
Upvotes: 0
Views: 94
Reputation: 4362
It's because you say if the e.target
has class name of "button"; that would only be true when you click in areas where the span element and its' parent don't overlap. When you click on the span element, it would say the "target" is the "span" element and it does not have the class name "button". Always do a console.log($(e.target));
to debug the issue.
Upvotes: 1
Reputation: 27765
Try to add .button
to your click selector:
$(function(){
$( ".artikel_box_lk .button" ).click(function( e ) {
window.open( "/lk/" + $( this ).attr("rel"), "_blank");
});
});
I suppose first time you click outside .button
div
Upvotes: 3