• Register
100 points
7 4

We have already seen some errors like “cannot read property of undefined”. This article discusses another one of them which is “cannot read property ‘preventDefault’ of undefined”.

Explanation

Here the javascript engine is telling us that it is unable to read the property of preventDefault it means it cannot use the default use of the property. For example see this code.

Cause

  function function1(e){
     e.preventDefault();
   } 
    <a href="#none" onclick="function1()">Click Me</a>

 

Here, we created a function with any parameter and it is supposed to do some work as it defines. When we call the function in the attribute tag, the javascript engine shows error that it cannot read that property due to some reason. 
And the reason is we passed the function in the attribute in a wrong manner. I mean we passed the function without the parameter. 
 

Solution

The solution for this error is very simple, we just have to pass the function correctly. We have to pass the function with its parameter.
We need to change 

    <a href="#none" onclick="function1()">Click Me</a>

 line to 

  <a href="#none" onclick="function1(event)">Click Me</a>
 

Hope this will help.
 

100 points
7 4