undefined variable and function on click is not working

I have problem in my jQuery code as shown below. I described here what we got problem in this.

var subnetCalculator = {
    _data: {
        _ipClass: $('#class_select'),
        _givenOctet1: $('#given_octet1_text'),
        _givenOctet2: $('#given_octet2_text'),
        _givenOctet3: $('#given_octet3_text'),
        _givenOctet4: $('#given_octet4_text'),
        _cidr: $('#given_mask_text')
    },
    initProblem: function(){
        var ipclass = this._data._ipClass.val();
        alert(ipclass); //ipclass variable is showing undefined.
    }
}

$("#problem_button").ready(function() { //This is not working on click function    
    console.log(5 + 6);
    subnetCalculator.initProblem();
});

Answers 1

  • Try the below. You should bind the function to click event.

    var subnetCalculator = {
      _data: {
        _ipClass: $('#class_select'),
        _givenOctet1: $('#given_octet1_text'),
        _givenOctet2: $('#given_octet2_text'),
        _givenOctet3: $('#given_octet3_text'),
        _givenOctet4: $('#given_octet4_text'),
        _cidr: $('#given_mask_text')
      },
      initProblem: function() {
        var ipclass = this._data._ipClass.val();
        alert(ipclass); * * //ipclass variable is showing undefined.**
      }
    }
    
    $(function() {
      $("#problem_button").click(function() {
        console.log(5 + 6);
        subnetCalculator.initProblem();
    
      });
    });
    

Related Articles