0

I would like to know how to make this code work: http://jsfiddle.net/LBXVd/2/. It's almost clean jqueryboilerplate, I'm interested in this part of code:

Plugin.prototype = {

    init: function() {

        /// create element <a> with click function
        myEl = $('<a/>', {  
            href: '#',
            text: '###',
           click: function(e){this._handler();}
    });
     $('ul').before(myEl);
    },


    //handler for click events on my <a> element...
    _handler: function(el, options) {
        alert("my handler called");
    }
};

How can I call function _handler after click on created element?

Thanks!

1 Answer 1

3

Your jsfiddle isn't very helpful since it doesn't show any result ... however I think it's because this in the this._handler() call is not what you think (it is the <a> element and not the prototype object).

Try

init: function() {

    var self = this; // ****

    /// create element <a> with click function
    myEl = $('<a/>', {  
        href: '#',
        text: '###',
       click: function(e){self._handler();} // ****
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your answer. I fixed my jsfiddle and tried your code. It works!

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.