-3

I use the following code snippet to invoke the click event of a document:

$(document).bind("click", $.proxy(proxy._cellClickHandler, proxy));

_cellClickHandler: function (e) {
            var $target = $(e.target),
                row = $target.closest('tr'),
                proxy = this,
                args = {},
                currentData,
                index;
}

Clicking it the first time does not fire the event. On the second click it invokes correctly. How to resolve this problem?

Jeff Bauer
  • 13,890
  • 9
  • 51
  • 73
Raja
  • 209
  • 1
  • 7
  • 16
  • wrap it inside $(document).ready() – techBeginner Feb 08 '14 at 06:23
  • 25 QUESTIONS AND NOT ACCEPTED ANSWERS AT ALL!? You really should [accept](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235#5235) some answers. – Teemu Feb 10 '14 at 09:28

2 Answers2

2

Use 'on' (http://api.jquery.com/on/):

var proxy= {
  _cellClickHandler: function (e) {
            var $target = $(e.target),
                row = $target.closest('tr'),
                proxy = this,
                args = {},
                currentData,
                index;
  }
}

$(document).on("click", $.proxy(proxy._cellClickHandler, proxy));
gurudeb
  • 1,856
  • 22
  • 29
0

Try using .live Click if you are not using the updated jquery 1.9

$(document).live("click", $.proxy(proxy._cellClickHandler, proxy));
Makrand
  • 587
  • 5
  • 14
  • Then why the second click working.? – Rajaprabhu Aravindasamy Feb 08 '14 at 04:18
  • 1
    Probably the element is loading dynamically. DOM is unable to register it in some way and therefore element might not be found. – Makrand Feb 08 '14 at 04:19
  • I am not the asker.. Just asked, ok if we assume that the element is not present while registering the event, Then why it is working on the second click. Two possibilities, either it should work correctly or it should not work completely. I think there might be some other problem with OP's code. – Rajaprabhu Aravindasamy Feb 08 '14 at 04:21
  • @RajaprabhuAravindasamy ya i got that later. The asker shared first four letters of your name i got confused. and to answer that question DOM and JS have to co-related. DOM has to load before JS. Dynamic elements tend to take time and posibility is that during second click that element is available. But again depends on the code. – Makrand Feb 08 '14 at 04:25
  • 1
    @Makrand - Or `.on` in new versions. – Derek 朕會功夫 Feb 08 '14 at 04:26