Inserting elements is nice and good to know,
but of course,
sometimes you want to remove elements and how do you remove
elements now?
Well, for dis you will have only two steps that you need to
execute.
You need to select the element that should be removed,
and then you need to remove it.
So let's see how that works.
Now, of course,
we do know how selecting an element of works and therefore
let's say we want to remove this H one element for this to
remove it. We first of all, need to select it.
And here all, just add a new variable first age,
one element,
and I can now get access to it by using document query
selector H one,
which will select me the first H one element.
It finds on the page, which is also our only H one element.
Alternatively I could of course have selected it by its ID.
That would be fine too.
Now, did we do have it selected?
We can remove it by simply using first H one element and
then calling the remove method on it.
And this method,
if called on an object on an HTML element object does just
what the name implies. It removes that object.
So if I saved that and I go back, this element is gone.
The H one element is gone and we can no longer see it there.
And alternative way of removing.
It would be to select its parent element.
So the body in this case,
and calling remove child on that element,
so on that body now,
and then pass that child that should be removed.
So the first age, one element to it,
this is way longer and more complex than doing it like this.
But I am mentioning it because this approach here using
remove does not work in older browsers,
like old versions of the internet Explorer.
And hence,
if you're building a website that should also work on those
super old browsers,
then this is a safer way of removing an element,
which will be supported on such older browsers as well.
So this is for older browsers.
Of course, if you're targeting modern browsers,
then using the shorter approach is better because it's
shorter and you can see removing elements is really
straightforward and not difficult.
Now here,
I'm getting an error because I'm trying to basically remove
a child that I did already remove before.
If I comment this out though, then dissolved.
So were X,
I got rid of the error and the H one element is deleted.
So this approach also works. You can use either of the two.