Remirlis Remirlis - 3 years ago 138
HTML Question

Why isn't my "click" event listener working for my delete button?

So i'm am trying to make a my button with the class of delete( it is being created via like 34 on my javascript.) So I set up an onclick the same way I did for my submit button but nothing happens when It is clicked. It has a simple console log to test if it is working. What is wrong with my code?

$(document).ready(function() {
$('#movie-search-submit').on('click', function(e) {
var search_term = encodeURIComponent($('#movie-title-search').val()); //gets value from element
var select_media = encodeURIComponent($('#media-select').val());
var url = '' + search_term + '&media=' + select_media;

var log_response = function(response) {
$.each(response.results, function(key, item) {

if (item.longDescription !== undefined) {
populate_listings(key, item);


$('.delete').on('click', function(e) {

var populate_listings = function(key, item) {
var result_row = '<div id="result-' + key + '" class="row"></div>';
var title = '<div class="api-result col-md-2 title"><h2>' + item.trackName + '</h2></div>'
var year = '<div class ="api-result col-md-4 year"><h4>' + item.releaseDate + '</h4></div>'
var description = '<div class ="api-result col-md-4 description"><p>' + item.longDescription + '</p></div>'
var delete_button = '<div class="btn btn-lg btn-primary col-md-2 delete">Delete</div>';
$('#result-' + key).append(title);
$('#result-' + key).append(year);
$('#result-' + key).append(description);
$('#result-' + key).append(delete_button);


method: "GET",
url: url,
dataType: 'jsonp',
success: log_response,

Answer Source

.delete button is not available when you are attaching the event to it. So delegate the event

$('body').on('click','.delete', function(e) {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download