0

I'm creating a custom step.js application for my project. I'm getting error on object scope. Below is my code

var STEPPED = {
    el: {
        stepContentDiv: $('#stepContent'),
        stepNavDiv:     $('#stepNav'),
        stepNavLi:      $('#stepNav li'),
        stepNavAnchor:  $('#stepNav li a'),
        stepNavDone:  $('#stepNav li a'),
        stepNavActive:  $('#stepNav li a')
    },
    doActive: function(){
        this.el.stepNavDone.on('click', function(){
            this.el.stepNavLi.each(function(){
                alert();
            });
        });
    },
    init: function(){
        this.doActive();
    }
}
STEPPED.init();

Getting error when I run this

this.el.stepNavLi.each(function(){
    alert();
});

Uncaught TypeError: Cannot read property 'stepNavLi' of undefined

Can anyone pleas help me what is the issue here? Thanks in advance

2
  • 1
    your code can't find this.el, that is undefined... Commented Oct 27, 2016 at 12:40
  • yes this is my question that why its undefined. But its defined at the top. cant understand Commented Oct 27, 2016 at 12:44

1 Answer 1

1

Within the click event handler function, this refers to the element that was clicked on, not the STEPPED object. You need to cache the reference to the object in a variable if you want to retain the reference to it within the click handler. Try this:

doActive: function() {
    var _stepped = this;
    _stepped.el.stepNavDone.on('click', function(){
        _stepped.el.stepNavLi.each(function(){
            alert();
        });
    });
},
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.