Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I get the index of an appended object in jQuery

$("input").keypress(function(e){
    let value = $("input").val()
    if(e.which == 13 && value != ""){
        $("input").val("")
        $(".itemCategRow:nth-child(1)").append("<div class=\"button\">" + value + "</div>")
    }
})

$(".button").clicked(function(){
    alert($(this).index())
})

nothing happens when I click the buttons, the reason for getting their certain indexes is to
remove certain buttons.

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Since the button is added dynamically, you can only listen to events on it through "event delegation". Try this

$(document).on("click", ".button", function(){
    alert($(this).index())
})
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading