Removing elements in JavaScript
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:
html<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:
javascriptlet elems = document.querySelectorAll('p');
for (let elem of elems) {
elem.addEventListener('click', function() {
elem.remove();
});
}
Задание 1
Given the following code:
html<ul>
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
Make it so that any li is
removed by clicking on it.
Задание 2
Given the following code:
html<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.