I have a list of products, each <li>
has a delete button and product's name
so for deleting the product i should affect a function to the created buttons i tried many methods but it did'nt work by using javascript file
but when i wrote the same code on the console of my navigator it works
so please tell me what's wrong
here is me jvascript code and html
thank you
var products = [];
//adding a prodicts
var button = document.querySelector("#add");
var name;
button.addEventListener("click", function() {
name = prompt("donner le type du produit");
products.push(name);
var ul = document.getElementById("products");
$("ul").append("<li><button class='delete'><i class='fa fa-trash'></i></button>" + name + "</li>");
});
//deleting a product using java script
/*var deleteButton = document.getElementsByClassName(".delete");
for (var i = 0 ; i < deleteButton.length; i++) {
deleteButton[i].addEventListener("click",function(){alert("you can delete this product now");}) ;
}
$(".delete").click(function(){alert("succ");});*/
$(function() {
$(".delete").click(function() {
alert("you can delete it now");
});
});
.form-inline {
text-align: right;
}
.navbar {
padding-top: 10px;
}
#list {
list-style-type: none;
margin: 0;
padding: 0;
}
ul {
list-style-type: none;
}
.delete {
background-color: #0000FF;
margin-right: 20px;
color: white;
text-align: center;
display: inline-block;
height: 30px;
width: 0;
transition: 0.2s;
opacity: 0;
}
li:hover button {
width: 30px;
opacity: 1.0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<form class="form-inline my-2 my-lg-0">
<button class="btn btn-primary my-2 my-sm-0" type="button">Résumé</button>
<button id="add" class="btn btn-primary my-2 my-sm-0" type="button">Ajouter</button>
<button class="btn btn-primary my-2 my-sm-0" type="button">Aide</button>
<button class="btn btn-primary my-2 my-sm-0" type="button">A propos</button>
</form>
</div>
</nav>
<ul id="products">
</ul>