Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plain JavaScript version of e.preventDefault();

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.

like image 675
Ricardo Zea Avatar asked Oct 04 '13 02:10

Ricardo Zea


People also ask

What is e preventDefault () in Javascript?

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.

What is the opposite of event preventDefault ()?

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.

What does the preventDefault () method in this code do?

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.

Do I need e preventDefault?

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.


4 Answers

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 image 127
PSL Avatar answered Oct 12 '22 06:10

PSL


// 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);
}
like image 25
Mike Samuel Avatar answered Oct 12 '22 04:10

Mike Samuel


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.

like image 1
Yuriy Galanter Avatar answered Oct 12 '22 05:10

Yuriy Galanter


yes you can use return false;

<a href="" onclick="return false;">test</a>
like image 1
Drixson Oseña Avatar answered Oct 12 '22 05:10

Drixson Oseña