Jquery Code / Events In Ajax-Refreshed Cgridview

I have placed a button, with jQuery’s binded on-click event, inside CGridView. Its HTML code sits in summaryText.

Button works only initially. After grid view is updated by AJAX (after filtering, sorting, pagination) it stops responding. I don’t know what causes this. Source code seems untouched and looks identically after grid view contents update like it was before it.

Is there any workaround for this problem or the only way is to disable AJAX-refresh of CGridView?

Thanks in advance for any help and have a nice weekend.

Looks like you might be better off using live() instead of bind() and depending on your jquery version maybe on(). Please check the accepted answer here:

Hope this helps

Yes, use .on() if you can. The .live() method has been deprecated.

Quick reply: I’m not using either of those, you specified.

I’m using simple $(’#button’).click();

I’ve heard (correct me, if I’m wrong), that if any Javascript / jQuery code will be injected into page content (DOM tree) via AJAX, it will not be executed / methods or events binding won’t be successful. And one must re-bind (bind again) these methods / events using code inside main page, after AJAX response is received and injected into DOM.

Am I right? Or is it a complete bullshit?

If you use .on(), you don’t need to worry about this. Outside of your gridview, try this:




<script type="text/javascript">

    $("body").on("click", "#button", function(){

        // your code here

    });

</script>



Keith, you’re genius! :]