Let's now learn how to remove elements. To do this,
use the remove
method. Let's look at an
example of how this is done. Let's have paragraphs:
<p>elem 1</p>
<p>elem 2</p>
<p>elem 3</p>
<p>elem 4</p>
<p>elem 5</p>
Let's make it so that any paragraph is removed by clicking on it:
let elems = document.querySelectorAll('p');
for (let elem of elems) {
elem.addEventListener('click', function() {
elem.remove();
});
}
Given the following code:
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
Make it so that any li
is
removed by clicking on it.
Given the following code:
<ul id="parent">
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
<input type="submit" id="button">
Make it so that on each click on the button,
the last element from #parent
is removed.