Null error is coming in javascript

Well this is one broken part (unless it’s intentional?)

Set the ID property to null

this.ID= null;

Try to access the property you just set equal to null

this.matchName= this.ID.substr(this.ID.lastIndexOf('_'));

This code is running in the initialization (constructor) of your Check class and will error out.


Here’s what I think you want, with better formatting so not to cause eye-bleeding.

// capitalize first letter of class name
function Check(className){ // use a prop/descriptive parameter name 
    var me = this; // set your variable "me" to class instanced
    this.ID = null; // INITIALIZE your ID and matchName variables
    this.matchName = null;

    this.first = function (){
        alert(me.matchName)
    };
    this.second = function (){
        alert(1)
    };
    this.touch = function (){
        // get the element with class=className
        // find the input inside of it
        // set an onkeyup handler to the input element
        $(className).find('input').keyup(function(e){
            me.ID = this.id;; // set the ID variable to the INPUT element's ID property
            // set matchName to the last part of the input's ID property
            // matchName will be "01" or "02" in this case
            me.matchName = this.ID.split("_")[this.ID.split("_").length - 1];
            if(e.keyCode==13){
                id.indexOf('first') > -1 ? me.first(): me.second();
            }
        });
    };
}
...
var first = new Check('.first');
var two = new Check('.two');
first.touch();
two.touch();

Leave a Comment