get the id of a dynamically generated tag

0

Good morning.

Does anyone know how to do in jquery to capture some data from a tag within a dynamically generated table ? In fact everything inside the "showPesqReserve" div is generated dynamically.

Ex:

<div id="showPesqReserva">
   <div id="calendar">
       <table id="tabela">
           <tr>
              <td><a role="button" class="dayReserve" data-dia="2016-01-15" data-local="sala1" data-horario="17:30">15</a></td>
           </tr>
       </table>
   </div>
</div>

I'm doing it this way but it's not working. It does not generate an error and nothing happens when clicking:

$("#tabela").on("click", "a", function(event){
    var iData = $(this).data("dia");
    var iLocal = $(this).data("local");
    var iHorario = $(this).data("horario");
    event.prevendDefault();
    $.ajax({
       type: "GET",
       url: "fazalgumacoisa.php",
       data: "dia="+iData + "&espaco="+iLocal + "&horas="+iHorario,
       dataType: "html",
       ...
    })
    return false;
});
    
asked by anonymous 28.01.2016 / 14:16

1 answer

0

Assuming your div is within the body tag of your html and that you already have the jquery library included in the code, do the following:

<script>
$(document).ready(function(){

    $("body").delegate(".dayReserve", "click", function(e){
        //Retirando ação padrão já no início do código.
        e.preventDefault()

        var iData = $(this).data("dia");
        var iLocal = $(this).data("local");
        var iHorario = $(this).data("horario");

        $.ajax({
           type: "GET",
           url: "fazalgumacoisa.php",
           data: "dia="+iData + "&espaco="+iLocal + "&horas="+iHorario,
           dataType: "html",
           ...
        })
        return false;
    });
})

</script>

.delegate() will allow other dynamically generated links without refresh tbm to be clicked. You can use .live() as well.

Remembering that your jQuery code should always be within $(document).ready(function(){...}); OR $(function(){...}); I hope I have helped !!!

    
28.01.2016 / 14:50