Refresh in PartialView misses JavaScript reference?

0

I have a JavaScript / Jquery method that updates a PartialView and there is a div with an associated fadeToggle fault.

JavaScript in Onload (to associate FadeToggle) (It works)

$("#ComentarioAba").click(function () {
    $("#Comentario").fadeToggle(500);
});

JavaScript method:

function ReloadComentarios() {
    $('#Comentarios').empty();
    $('#Comentarios').load(document.URL + ' #ComentarioAba');

    $("#ComentarioAba").click(function () {
        $("#Comentario").fadeToggle(500);
    });
}

HTML code

<div id="Comentarios">
   <div id="ComentarioAba" class="col-md-12 mwTituloArea">
      <p>Comentários </p>
   </div>
   <div id="Comentario" class="col-md-12">
      <div class="col-md-12 ">
        ...
      </div>
   </div>
</div>

It turns out that after the ReloadComentarios method is executed, fadeToggle no longer works. Even the association being redone within the method itself.

Why does this occur?

    
asked by anonymous 23.03.2017 / 01:54

1 answer

0

You do not actually lose the reference.

It happens that Load only updates the ID Div #ComentarioAba configures the call:

$('#Comentarios').load(document.URL + ' #ComentarioAba');

To correct it, I had to "nest" another div and it looked like this:

<div id="Comentarios">
   <div id="ComentariosReload">
      <div id="ComentarioAba" class="col-md-12 mwTituloArea">
         <p>Comentários </p>
      </div>
      <div id="Comentario" class="col-md-12">
         <div class="col-md-12 ">
           ...
         </div>
      </div>
   </div>
</div>

Then update JavaScript to give Reload in ComentariosReload :

$('#Comentarios').load(document.URL + ' #ComentarioAba');

So the JavaScript reference was not getting lost, what was lost was the Div that was no longer loaded / processed and therefore Toogle would not work, since the DIV did not exist anymore.

    
23.03.2017 / 21:22