Jquery with dynamic html code

Asked

Viewed 171 times

0

I have an Ajax function that when added, inserts/ updates a page. php

 $("#recarrega-adt").click(function(event) {
   $.ajax({
                 url       : 'classes/consulta-aditivo.php',

            success : function(txt){
                 $('#view-full-adt').load('classes/consulta-aditivo.php');
            },
            error: function(result) {
                alert("Erro");
                /*sweet alert*/
            }
  });
 }); 

The problem is that when this action is performed the other JS/Jquery functions stop working, as this code was inserted "after" the rest was executed. How should I solve this problem? I found the method live() and on(), however both did not take effect... The only way I found was to leave the method on the same page that is called but would not like to do this =/

Thank you in advance.

$(document).ready(function(){
$(".option-aditivo h3").on("click",function(){
    alert('teste');
    var id = this.id;
    var frase = this.title;
    $("<span class='frase-aditivo mover' id='adt"+id+"-enable'>"+frase+"</span>").appendTo("#papel");

    $(this).removeAttr('id')
    $(this).attr('id',id+"-plus");
    Draggable.create("[id*=adt]");
  });
 });
  • the code is within $(Document). ready(Function(){//seuajax}) ?

  • you can show how the other functions are being called?

  • @Lucaoa Are Yes...

  • @Tafarel_brayan Hello, Follow function code to be executed.

  • vc can do as follows instead: $(". option-additive H3"). on("click", Function(){} do this: $(Document). on("click", ". option-additive H3", Function(){}...

  • @user48796 already checked if the console is not showing any error ? Because depending on the error, it is very likely that "lock" your javascript

Show 1 more comment

1 answer

0

Instead of wearing it like this:

$(document).ready(function(){
    $(".option-aditivo h3").on("click",function(){
        alert('teste');
        var id = this.id;
        var frase = this.title;
        $("<span class='frase-aditivo mover' id='adt"+id+"-enable'>"+frase+"</span>").appendTo("#papel");
        $(this).removeAttr('id')
        $(this).attr('id',id+"-plus");
        Draggable.create("[id*=adt]");
   });
});

Try so:

$(document).ready(function(){
    $(document).on("click", ".option-aditivo h3", function(){
        alert('teste');
        var id = this.id;
        var frase = this.title;
        $("<span class='frase-aditivo mover' id='adt"+id+"-enable'>"+frase+"</span>").appendTo("#papel");
        $(this).removeAttr('id')
        $(this).attr('id',id+"-plus");
        Draggable.create("[id*=adt]");
   });
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.