Cannot read property ‘preventDefault’ of undefined in javascript error

In Console I got following error using e.preventDefault() method
I used e as a function parameter

  function function1(e){

1533 Uncaught TypeError: Cannot read property ‘preventDefault’ of undefined.

Called function1 like

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

You have to pass event in the used function:

function1(event); // where you called it 

For example:

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

Make sure you call this function within an event handler. Such as :


I remove event from function and invoke function in this way:

<button class="btn btn-primary" runat="server" id="btnSave" type="submit"                                           
          onserverclick="btnSave_OnServerClick"  onclick="return 

In JavaScript:

function jsFunction() {
        if ($('#form1').bootstrapValidator('validate').has('.has-error').length) {
            alert('SOMETHING WRONG');             
        } else {
            alert('EVERYTHING IS GOOD');
            __doPostBack('<%=btnSave.UniqueID%>', '');
        return false;

You are writing the function wrong. Suppose you are using function on a particular button click having id as ‘clickBtn’ then you need to write function like this.

$("#clickBtn").on("click", function(e){

You failed to pass the event as a parameter in your in luck event in the html.
So it should be written as the sample below:

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

function function1(event){

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .
Read More:   Uncaught ReferenceError: jQuery is not defined [duplicate]

Similar Posts