I have a list-group with list-group-items in it in the form of buttons. I would like to add clickable icons to each list-group-item, for example delete buttons. However, when adding an icon to a list-group-item I can either prepend the icon to the list-group-item or place it before the item. When I prepend the icon, it comes out like this. Clicking the delete icon triggers the click event for the entire list-group-item, not the click event for the delete icon.
Javascript where I add the list-group-items and the delete icon(button):
var button = $('<button/>').text(result[key].deviceId).addClass('list-group-item device').attr({name:result[key].deviceId, "aria-label": "Quick View Device", "data-toggle": "modal", "data-target": "#quick-view-device-modal", type: "button"});
var deviceName = result[key].deviceId;
var deleteButton = $('<button/>').attr('type','button').attr('name','delete').attr('id',deviceName).html('<span class="glyphicon glyphicon-minus-sign red"></span>').addClass('icon delete-device');
button.prepend(deleteButton);
$('#device-list').append(button);
This code ends up adding the delete icon button within the list-group-item button, as a child element. I believe that is why clicking the delete icon button triggers the click event for the entire list-group-item. Because of this, I tried another way:
Javascript for adding delete icon before list-group-item:
var button = $('<button/>').text(result[key].deviceId).addClass('list-group-item device').attr({name:result[key].deviceId, "aria-label": "Quick View Device", "data-toggle": "modal", "data-target": "#quick-view-device-modal", type: "button"});
var deviceName = result[key].deviceId;
var deleteButton = $('<button/>').attr('type','button').attr('name','delete').attr('id',deviceName).html('<span class="glyphicon glyphicon-minus-sign red"></span>').addClass('icon delete-device');
$('#device-list').append(button);
$('button[name='+deviceName+']').before(deleteButton);
$('.delete-device').hide();
When I add the icon before each list-group-item, it comes out like this. The icon does nothing when I click it. First of all, if I add the delete icon like this how would I make the icon line up with the item it corresponds with? Secondly, why is the click event for the icon not being triggered when I click it?