Reputation: 4605
I've got a working a.click() function in jquery... But if I click an anchor, I open an new window... But how can I stop the browser from opening a new window itself??
example:
$('a').each(function() {
$(this).click(function(e) {
if($(this).attr('target') == '_popup') {
//here is code to open window (already exists)
//try to stop propagation, but doesn't work...
e.stopPropagation();
}
hideNSL();
});
});
so, how do I stop the event??
Upvotes: 5
Views: 11637
Reputation: 350
EDIT, I stand corrected .preventDefault is not a jQuery function.
Upvotes: -1
Reputation: 17792
Try
e.preventDefault()
but here return false
may also do it which is in the other answer. preventDefault
can be used in more senarios and is a more generic "stop the default event action", see: http://api.jquery.com/event.preventDefault/
Upvotes: 12
Reputation: 15221
Add this to the end of your click event:
return false;
So for example:
$('a').each(function() {
$(this).click(function(e) {
var toReturn = true;
if($(this).attr('target') == '_popup') {
//here is code to open window (already exists)
toReturn = false;
}
hideNSL();
return toReturn;
});
});
Upvotes: 4