With the following jQuery based script you can stop the default action of dummy links, ie: <a href="#">
$('a').click(function(e) {
e.preventDefault();
});
What would be the plain vanilla version of this script?
I'm not a JavaScript programmer but I'm thinking it may be something that uses return false;
. Again, I may be totally wrong.
Thanks in advance for your help.
The preventDefault() method cancels the event if it is cancelable, meaning that the default action that belongs to the event will not occur. For example, this can be useful when: Clicking on a "Submit" button, prevent it from submitting a form. Clicking on a link, prevent the link from following the URL.
There is no opposite method of event. preventDefault() to understand why you first have to look into what event. preventDefault() does when you call it. Underneath the hood, the functionality for preventDefault is essentially calling a return false which halts any further execution.
preventDefault() The preventDefault() method of the Event interface tells the user agent that if the event does not get explicitly handled, its default action should not be taken as it normally would be.
If you don't use e. preventDefault() , the default behaviour of form submit will fire. It will send browser to the action property of form and browser will disappeared that you don't want it.
You have event.preventDefault() available in vanilla javascript as well. In a generic way you can always use return false
. If you attach an event handler you are guaranteed to get event agument passed in the handler as opposed to using the onclick
or any other attributes of the element (In which case you should rely on the specific event object available in side the handler which you may not get in all browsers, like in IE you would use window.event
).
Ex: -
document.getElementById('someId').addEventListener('click', function(e){ //say this is an anchor
//do something
e.preventDefault();
});
So for all the anchors:
var anchors = document.getElementsByTagName('a');
for(i=0, len=anchors.length; i<len; i++){
anchors[i].addEventListener('click', function(e){e.preventDefault();});
}
// Like $('a'), gets all the <a> elements in the document.
var aElements = document.getElementsByTagName('a');
// Create one function object instead of one per <a> element.
// The calling convention is the same for jQuery as for regular JS.
function preventDefaultListener(e) { e.preventDefault(); }
// For each a element,
for (var i = 0, n = aElements.length; i < n; ++i) {
// register the listener to be fired on click.
aElements[i].addEventListener('click', preventDefaultListener);
}
Return false is the way to go. E.g. if your anchor already has an href:
<a href="http://www.google.com" onclick="alert('clicked!');return false">Click me</a>
calling onclick like this will only perform the function, but not the navigation.
yes you can use return false;
<a href="" onclick="return false;">test</a>
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