Gigaxel Gigaxel - 4 days ago 5
Javascript Question

How to add event handlers to dynamically created buttons in jQuery?

I used AJAX to dynamically create the HTML but I've encountered a problem

<script>
function page_loaded(){
jQuery.ajax({
method: "GET",
url: "get_data_dashboard.php",
success: function(data){
var markers = JSON.parse(data);
for(var i = 0; i < markers.length; i++){
var m = markers[i];
var markerHTML = "<div class='marker'>" +
"<span id='naziv'>Naziv zahtjeva: " + m.naziv + "</span></br>" +
"<span id='ulica'>Ulica: " + m.ulica + "</span></br>" +
"<p id='opis'>Opis:</br>" + m.opis + "</p></br>" +
"<span id='email'>Email: " + m.email + "</span></br>" +
"<img id='slika' src='" + m.link_slike + "' />" + "</br>" +
"<textarea rows='5' cols='30' maxlength='500' id='t" + m.marker_id + "' placeholder='Komentar'>" + "</textarea></br>"
+ "<div class='buttons'><a href='odobri_prijavu.php?id=" + m.marker_id + "'>Odobri</a>" +
"<a href='izbrisi_prijavu.php?id=" + m.marker_id + "'>Izbriši</a>" + "</div>" +
"</div><hr>";

$('#content').append(markerHTML);
}

}
})



}
$(document).ready(page_loaded());

</script>


I tried to use buttons first instead of anchor tags but I couldn't figure how to add event handlers to dynamically created buttons that will post a request via AJAX to some php script with the proper id as the value and the value of the textarea. So I used the anchor tag and I was able to send the id, but I can't send the value of the textarea because I don't know how to reference it and even if I referenced it, it will be NULL because its value is set to the anchor tag at the very beginning and I want to type in text in the textarea.

Answer
<script>
    function page_loaded(){
        jQuery.ajax({
            method: "GET",
            url: "get_data_dashboard.php",
            success: function(data){
                var markers = JSON.parse(data);
                for(var i = 0; i < markers.length; i++){
                    var m = markers[i];
                    var markerHTML = "<div class='marker'>" +
                        "<span id='naziv'>Naziv zahtjeva: " + m.naziv + "</span></br>" + 
                        "<span id='ulica'>Ulica: " + m.ulica + "</span></br>" + 
                        "<p id='opis'>Opis:</br>" + m.opis + "</p></br>" + 
                        "<span id='email'>Email: " + m.email + "</span></br>" + 
                        "<img id='slika' src='" + m.link_slike + "' />" + "</br>" + 
                        "<textarea rows='5' cols='30' maxlength='500' id='t" + m.marker_id + "' placeholder='Komentar'>" + "</textarea></br>"
                        + "<div class='buttons'><a href='odobri_prijavu.php?id=" + m.marker_id + "' onclick="clickHandler('"+m.marker_id+"')">Odobri</a>" +
                            "<a href='izbrisi_prijavu.php?id=" + m.marker_id + "'>Izbriši</a>"  + "</div>"  + 
                        "</div><hr>";

                    $('#content').append(markerHTML);
                }

            }
        })



    }
    $(document).ready(page_loaded());
    function clickHandler(id){
      $('#'+id) // selects the button
      $('#t'+id) // selects the text area
    }
</script>
Comments