Skip to content Skip to sidebar Skip to footer

Class Is Not Defined Onclick Event

I use the new class syntax for JS. When I press a button, I want to call a method from a class. To archieve this, I set this method static. class NoteController { // The controller

Solution 1:

EDIT: Correction to make it static:

<script>classNoteController { // The controller classconstructor() { // Initialization// ...alert('constructor called');
    }

    staticCreateNote() { // The method to call - static// ....alert('create note');
    }
}
//var nc=new NoteController();//console.log(nc);</script><buttontype="button"id="btnCreateNote"onclick="NoteController.CreateNote()">Create</button> // call the method

JSFiddle

This is the working example of your code:

<script>classNoteController { // The controller classconstructor() { // Initialization// ...alert('constructor called');
    }

    CreateNote() { // The method to call - static// ....alert('create note');
    }
}
var nc=newNoteController();
console.log(nc);
</script><buttontype="button"id="btnCreateNote"onclick="nc.CreateNote()">Create</button> // call the method

Working JSFiddle

Solution 2:

Since lack of information about NoteController.js,I assume that your NoteController.js has below code.

function NoteController()
    {
    //variables//sub functionsfunction CreateNote()
     {
      //code
     }
    }

So now you need to create the object of the function and call it.

<scriptsrc="NoteController.js"></script><script>var _noteController=newNoteController();
</script><body><buttontype="button"id="btnCreateNote"onclick="_noteController.CreateNote()">Create</button></body>

Solution 3:

Include your class inside the <script> tags, So that the browser will identify the javascript code and execute it!

Post a Comment for "Class Is Not Defined Onclick Event"