Ken
Ken

Reputation: 529

How to read class properties outside of jQuery?

I've wrote a JS class and I want to make use of jQuery's API but don't know how to read the values outside of the jQuery object.

function Slideshow( originalslideshowid, nextslideshowId ) {

    this.originalslideshowId = originalslideshowid;
    this.nextslideshowId = nextslideshowId;

    $("#nextslideshow").click( function(e) {

        hideoriginal( this.originalslideshowId );

    } );

    this.showOriginal = function() {

        $( this.originalslideshow ).click(function(e) {

            shownext(this.nextslideshowId);

        });

    };

};

As you can guess, this.originalslideshowId and this.nextslideshowId returns undefined as they are trying to read the properties from within the jQuery object. How can I get them to read the correct properties from the Slideshow class?

Upvotes: 0

Views: 57

Answers (1)

Jason P
Jason P

Reputation: 27012

Save this to a variable:

function Slideshow( originalslideshowid, nextslideshowId ) {
    var _this = this;

    this.originalslideshowId = originalslideshowid;
    this.nextslideshowId = nextslideshowId;

    $("#nextslideshow").click( function(e) {    
        hideoriginal( _this.originalslideshowId );    
    });

    this.showOriginal = function() {    
        $( this.originalslideshow ).click(function(e) {    
            shownext(_this.nextslideshowId);    
        });    
    };    
}

Upvotes: 1

Related Questions