This is my first SO question :)
I've dynamically created buttons from an array, and am trying to attach an onclick event to each of them. So far, I haven't been able to log any reaction from them. How would I target a specific button in the set?
Thank you! `
var topics = ["Amy Winehouse", "Aretha Franklin", "Beyonce","Bjork", "Barbara Streisand", "Britney Spears", "Cher","Christina Aguilera", "Celine Dion", "Diana Ross","Donna Summer","Debbie Harry","David Bowie","Dolly Parton","Erykah Badu","Jennifer Lopez","Lana del Rey","Lady Gaga","Missy Elliot","Madonna","Mariah Carey","Shakira","Stevie Nicks","Whitney Houston"];
var buttons;
//function to create buttons
function divaBtns(){
for (var i=0; i < topics.length; i++){
buttons = $('<div></div>');
buttons.html("<input type='button' id='buttons' value='" + topics[i] + "'/>");
$("#btns").append(buttons);
}
}
divaBtns();
$('#btns').on('click', 'button', function(){
console.log("click");
});
<!DOCTYPE html>
<html>
<head>
<!--chart.js-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="./assets/giphy.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" integrity="sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszuE4W1povHYgTpBfshb" crossorigin="anonymous">
<link rel="stylesheet" href="./assets/style.css">
<title>Diva Gifs</title>
</head>
<body class="body">
<div class="container">
<div id="btns"></div>
<div id="gifs"></div>
</div>
</body>
</html>
`
var button = $('<div></div>');inside the loop maybe?id. Maybe you could just add aclass='diva-btn'instead?