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??
Using onclick Event: The onclick event attribute works when the user click on the button. When mouse clicked on the button then the button acts like a link and redirect page into the given location. Using button tag inside <a> tag: This method create a button inside anchor tag.
If you are trying to trigger an event on the anchor, then the code you have will work I recreated your example in jsfiddle with an added eventHandler so you can see that it works: $(document). on("click", "a", function(){ $(this). text("It works!"); }); $(document).
Answer: Use the jQuery click() Method You can use the click() method to trigger a click on a link programmatically using jQuery.
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/
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;
});
});
You could try
e.preventDefault();
e.preventDefault();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With