Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use Jquery- $.remove() when Re-Bind?

I have html table which was generated from server side :

           DataTable dt2 = new Claims_Service().ASO_MOD_Get_Nulls();
           myGridView.DataSource = dt2;
           myGridView.DataBind();

The table has rows and in each row there is a button.

in client side I write :

 $(".myGridView").on('click', '.myButton', function ()
        {
          ...
        }

Now lets say I need to re-bind in server side. ( rebind === full postback and regenerate page)

Should I use jQuery remove function in order to release the events and prevent memory leaks before I rebind ?

Also , Would your answer will be different if I wrote :

$(".myGridView .myButton").on('click',function ()
like image 209
Royi Namir Avatar asked Nov 16 '25 17:11

Royi Namir


2 Answers

It's not necessary to remove handlers when you are reloading the page via a full POST or GET request. In any event, remove() will remove the elements from the DOM, not simply remove the event handlers. To remove event handlers you want to use off().

like image 188
tvanfosson Avatar answered Nov 19 '25 07:11

tvanfosson


If written like so:

$(".myGridView").on('click', '.myButton', function () { /* your code */ });

memory leaks are not a concern as you are pattern matching on dom bubble to all the ".myButton" occurances.

while

$(".myGridView .myButton").on('click',function () { /* your code */ });

is attaching to all the indivual ".myButton" occurrences

like image 41
King Friday Avatar answered Nov 19 '25 06:11

King Friday



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!