Reload functions after ajax response

0

When I write a comment on my page, I generate an ajax to save the comment in the database, and the response is the comment in html, this same comment has similar tags to the next one.

<a href="#" action="delete">Borrar</a>

I have the action function in the .js file

$('a[action]').on('click',function(e){
     e.preventDefault();
     // Resto de la función...
});

The problem is that by entering the code in the html document that function does not work, forgive the redundancy.

I enter the code with a .append () in the success: function () of ajax

$.ajax({..., success:function(e){
     $('#div').append(e.codigo);}});

How do I avoid having to reload the entire .js file?

    
asked by Ivan Alcaide 06.11.2016 в 21:42
source

1 answer

0

Friend, I am not entirely clear about the functionality. However, if you want the link with "action = delete" to work, after loading it dynamically with ajax, you must use the delegation of events that jquery offers (with the on method).

In any case, if it is that, in your JS code, you would have to put something like this:

$("#contenedor-comentarios").on('click','a[action="delete"]',tuFuncion)

This assuming that # container-comments is the ID of the parent div where you add the comments. It must be a div that is not replaced and exists from the moment you load the page, so jquery can identify if there are changes within it (when you add a comment with its respective action to delete) and you can activate the function on the link with "action = delete". PS: Suddenly be better, than in the action, add a real link and manage the functionality with a data field.

I hope it serves you, Greetings.

    
answered by 06.11.2016 / 22:02
source