1
When the document is ready, $(document).ready
, I send an AJAX request to a PHP page that generates a table that I put in my body, but,... below this ajax request, I have functions that handle events in this table, such as hover
in the row, click the button that is in the generated table, and so on.. ), but the functions related to the table are not working, the code is OK, no syntax errors.
I want to know if, by dynamically generating the table, when I associate events with it, it may be that events are not assigned because it has not yet been generated or something like that?
Code of the ajax request:
$(document).ready(function(){
$(function attform() {
$(".table").find("tbody").empty();
$.ajax({
url:"action/action.php",
method:"POST",
data:{acao:"attform"},
success: function(data){
$("tbody").append(data);
}
}); // fim ajax
}); // fim attform()
});
</script>
Right after that, I have a js.js document that basically has functions like this:
$(document).ready(function () {
$(".modificar").hover(
function () {
these = $(this);
$(these).parent().css("background-color", "blue");
},
function () {
$(these).parent().css("background-color", "white");
});
});
See that, $(".modificar")
, is a class of a button that will be generated dynamically by the ajax request I made up there..
It looks like a typical case where you need to delegate events. You can add the code you refer to?
– Sergio
http://answall.com/q/36811/129
– Sergio
@Sergio, I edited and put the scripts, but look, if this is the problem, I can then replace with
$(".modificar").on(hover,function(){},function{});
?– Ale
application of the method
on()
then it is to place events on objects that can, or not, were created dynamically?– Ale
@Alexandrec.Caus Não, depends on how the
.on()
is used– bfavaretto
That should be done with CSS... you don’t want to go that way?
– Sergio
I really should, but I did it this way to escape the conventional! @bfavaretto, correct me if I’m wrong and need to read more.. But, the on() selector must exist, whereas the second parameter is the one that was generated dynamically, isn’t it? Type:
$(deveexistir).on(evento,vaisergeradodinamicamente,function(){...});
– Ale
@Alexandrec.Caus Exactly.
– bfavaretto