0

I Can't understand which is the problem of my code trying to click on a list-group-item of my list-group. I want to catch the click and nothing's happening.

$("#inserisci_cerca").keyup(function() {

        var text = $("#inserisci_cerca").val();

        $.ajax({url:"https://ewserver.di.unimi.it/mobicomp/geopost/users?session_id="+sessionStorage.getItem("session_id")+"&usernamestart="+testo+"&limit=10",
                type: "GET",
                dataType: "json",
                success: function(result) {


                    $("#div1").html("");
                    $("#div1").append("<ul class='list-group'>");
                    result.usernames.forEach(function(item,index) {
                        $("#div1").append("<button type='button' class='list-group-item list-group-item-action' id='okok'>" + item + "</button>");
                    })
                    $("#div1").append("</ul>");
                }
        });
    });

$("#okok").click(function() {
 console.log("Hello");
})

What am i doing wrong?

  • Learn [Event Delegation](http://learn.jquery.com/events/event-delegation/) and as in HTML you can't have duplicate identifiers use class selector i.e. `$("#div1").on('click', '.list-group-item-action', function() { console.log("Hello"); })` – Satpal Sep 10 '18 at 12:04
  • 'Mazing! Thank you! – Mattia Puntarello Sep 10 '18 at 12:13

0 Answers0