With the help of jQuery, its easy to remove any html within a single click. jQuery is having 2 methods to remove html elements.
- remove() - Removes the selected element (and its child elements)
- empty() - Removes the child elements
jQuery remove() Method
This method removed the html element with its child element.
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script> // calling jquery min js script
<script>
$(document).ready(function(){
$("button").click(function(){
$("#div1").remove(); // function to remove html of div id =1
});
});
</script>
</head>
<div id="div1" style="height:50px;width:50px;bbackground-color:blue;"> // html of the text
text 1
<p>text 2</p>
</div>
<button>Remove above html</button> // button
jQuery empty() Method
The jQuery empty() method empty the selected element (removes only child HTML element)
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#div1").empty(); // function to empty the di of id=1
});
});
</script>
</head>
<body>
<div id="div1" style="height:50px;width:50px;background-color:blue;">
text 1
<p>text 2</p>
</div>
<br>
<button>Empty the html box</button> // button
0 Comment(s)