lundi 29 mars 2021

Jquery load function don't execute javascript the second time

Well, here is a mysterious problem, i'll explain :

I want to load a page into a div with the jquery load function. In my included file, i have some html, and a js script. My script, in a dom ready, (for test purposes) replace the content of a p balise.

I load this page on a button click event.

When i click it the first time, it all works. When i click the second time, all works, except that the javascript isn't executed.

What i tested :

  • Using dom ready or not (no difference)
  • Removing php in my include file (no difference, and i was desperate lol)
  • Removing success callback function from load method (again, no difference)

index.php (simplified):

<div id="truc">
</div>
<script>
$(function () {
    $('button#test').on('click', function () {    
        $('#truc').load('truc.inc.php');
    });
});
</script>

truc.inc.php :

<div>
    <p id="brebis">Je suis une brebis</p>
</div>

<script>
    $(function () {
        $('#brebis').text($('#brebis').text() + " égarée");
    });
</script>

First time it shows "Je suis une brebis égarée", second time "Je suis une brebis"

Do you guys have an idea ?




Aucun commentaire:

Enregistrer un commentaire