views:

54

answers:

1
<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 I find caller from here ?
   }
  }
 };
 jObject.bind();
</script>

UPDATE

I read some trick from here - http://www.mennovanslooten.nl/blog/post/62

And now I can get 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 this
                return function(){      // 3. wrap with return function(){ ... }
                    alert(jObj);        // 4. now I can get jObject here.
                }
            }(this);                    // 2. and this
        }
    };
    jObject.bind();
</script>
A: 

Inside your onclick, this will refer to the <input id="btn"> element you clicked, for example:

var jObject = {
 bind : function(){
  var o = document.getElementById('btn');
  o.onclick = function(){
   alert(this.value); //alerts 'click'
  }
 }
};
jObject.bind();
Nick Craver
Thanks Craver, And is it possible if I wannna get jObject from that inner function, too ?
diewland