How to delete the selected Div
here is simple image gallery Program . When we Click that particular div will be deleted . it will delete all the div until the last Element.
Here is the Code
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<style>
#box1, #box2, #box3, #box4, #box5 {
width:150px;
height:150px;
border:1px solid red;
display:block;
}
li {
display: inline;
}
#box1{
background-color:red;
}
#box2{
background-color:blue;
}
#box3{
background-color:pink;
}
#box4{
background-color:green;
}
#box5{
background-color:yellow;
}
</style>
</head>
<body>
<div id="myList">
<ul>
<li>
<div id="box1"></div>
</li>
<li>
<div id="box2"></div>
</li>
<li>
<div id="box3"></div>
</li>
<li>
<div id="box4"></div>
</li>
<li>
<div id="box5"></div>
</li>
</ul>
</div>
<script>
document.querySelector("#myList").addEventListener('click', function (e) {
var removeNode = e.target.parentNode;
console.log(removeNode)
console.log(this)
var howMany = this.querySelectorAll('li').length;
console.log(howMany)
if (howMany > 1) {
removeNode.parentNode.removeChild(removeNode);
}
else {
alert("This is last Element .. YOU Have to select THis One")
}
})
</script>
</body>
</html>
Here is the Code
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<style>
#box1, #box2, #box3, #box4, #box5 {
width:150px;
height:150px;
border:1px solid red;
display:block;
}
li {
display: inline;
}
#box1{
background-color:red;
}
#box2{
background-color:blue;
}
#box3{
background-color:pink;
}
#box4{
background-color:green;
}
#box5{
background-color:yellow;
}
</style>
</head>
<body>
<div id="myList">
<ul>
<li>
<div id="box1"></div>
</li>
<li>
<div id="box2"></div>
</li>
<li>
<div id="box3"></div>
</li>
<li>
<div id="box4"></div>
</li>
<li>
<div id="box5"></div>
</li>
</ul>
</div>
<script>
document.querySelector("#myList").addEventListener('click', function (e) {
var removeNode = e.target.parentNode;
console.log(removeNode)
console.log(this)
var howMany = this.querySelectorAll('li').length;
console.log(howMany)
if (howMany > 1) {
removeNode.parentNode.removeChild(removeNode);
}
else {
alert("This is last Element .. YOU Have to select THis One")
}
})
</script>
</body>
</html>
Comments