I am having a hard time getting my delete button to remove an item dynamically. When I add a new ingredient the button appears but does not have functionality.
$(document).ready(function () {
getRecipe();
$(".btn-primary").on("click", addIngredient);
$(".btn-default").on("click", deleteIngredient);
});
var recipe = {
title: "Pizza",
servings: 4,
ingredients: ["cheese", "sauce", "flour", "pepperoni"]
};
var button = "<button type=\"button\" class=\"btn btn-default delete btn-xs\"><span class=\"glyphicon glyphicon-minus-sign\"></span></button>";
function getRecipe() {
$("#title").append(recipe.title);
$("#servings").append(recipe.servings);
for (var i = 0; i < recipe.ingredients.length; i++) {
$("#ingredients ul").append("<li>" + recipe.ingredients[i] + " " + button + "</li>");
};
};
function addIngredient() {
var newIngredient = $(".form-control").val();
$(".form-control").val(newIngredient);
$("#ingredients ul").append("<li>" + newIngredient + " " + button + "</li>");
};
function deleteIngredient() {
$(this).closest("li").remove();
};