javascript - How can I find caller from inner function? -


<input type='button' id='btn' value='click' />  <script type="text/javascript">  var jobject = {   bind : function(){    var o = document.getelementbyid('btn');    o.onclick = function(){     // how can find caller here ?    }   }  };  jobject.bind(); </script> 

update

i read trick here - http://www.mennovanslooten.nl/blog/post/62

and can jobject inside inner function.

<input type='button' id='btn' value='click' />  <script type="text/javascript">     var jobject = {         bind : function(){             var o = document.getelementbyid('btn');             o.onclick = function(jobj){ // 1. add                 return function(){      // 3. wrap return function(){ ... }                     alert(jobj);        // 4. can jobject here.                 }             }(this);                    // 2. ,         }     };     jobject.bind(); </script> 

inside onclick, this refer <input id="btn"> element clicked, example:

var jobject = {  bind : function(){   var o = document.getelementbyid('btn');   o.onclick = function(){    alert(this.value); //alerts 'click'   }  } }; jobject.bind(); 

Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -