javascript - How can i use two different scope "this" in one function? -


so need inner text of given element through jquery event, , set text member of class e.g.

myclass = function () {     this.index = 0;      this.onnavelementclick = function ()     {         this.index = parseint(this.text());     }      this.mymain = function ()     {         $("nav#wow-so-much-inspiration").on("click", "a", this.onnavelementclick);     } }  myobject = new myclass(); myobject.mymain(); 

html:

<nav id="wow-so-much-inspiration">     <a href="#">1</a>     <a href="#">2</a>     <a href="#">3</a> </nav> 

but won't work because of 2 different scopes inside onnavelementclick() function... , don't idea of doing _this = this, i'm pretty sure there right way without doing macgyver coding.

jquery event handlers take event object (including target on event triggered) first argument. can use $.proxy have event handler bound outer this.

something this:

this.onnavelementclick = $.proxy(function (e) {     this.index = parseint($(e.target).text()); }, this); 

Comments

Popular posts from this blog

php - Zend Framework / Skeleton-Application / Composer install issue -

c# - Better 64-bit byte array hash -

python - PyCharm Type error Message -