For whatever reason, an element can’t destroy itself in JavaScript. jQuery has a method for this, which is nice because this is how we think:
$(".remove-me").remove();
But there is no direct equivalent in JavaScript. Instead you’ll need to select the parent element and use removeChild
.
So if you have:
<div class="module">
<p>Stuff.</p>
<div class="remove-me">...</div>
</div>
You’ll need to do:
var thingToRemove = document.querySelectorAll(".remove-me")[0];
thingToRemove.parentNode.removeChild(thingToRemove);
Or if you had a reference to an element and wanted to empty out all the elements inside it, but keep it:
mydiv = document.getElementById('empty-me');
while (mydiv.firstChild) {
mydiv.removeChild(mydiv.firstChild);
}
How about using a facade pattern for this task?
+1 for the wrapper :)
Nice but to difficult for most html, css and jquery people.
Nah you’ll be OK.
Think of it like this:
Hi,
I am trying to hide a class in a page when a particular class is in that page. Other wise the class should reflect.
When the class “gsg-nav” is present i wish to hide this class “article-sidebar side-column”.
How could i do that.
Below is the link for my code.
http://codepen.io/anon/pen/MwgNVx
Hey Ren, you may get better luck posting this question to the forums since there is a larger group of people available to help there.
Best of luck!
fghfhfh
qwwwwwwwwwwwww
Hello Guy’s ,
anyone can help me on a things , I have in a page once
<nav class='test'>
and below a<div>
without any class/id, I want to hide/remove<div>
element which is in next of<nav>
tag , I can do$('.test').next().remove();
but strictly jQuery is not allowed , please give me any trick of JavaScript or CSS, Please note that , i haven’t access to edit any html code only i can add js/css.Amit,
I am having trouble embedding code in here, but I put together a quick example which seems to do what you want. demo
Hi, im trying to filter the content of a web site depending on the type of user, and i think that this is a good way to do, my question is how this method affect the performance of the website? because if i use display none, the element exist in the DOM and affect the performance, thanks! sorry for my english
why you guys have taken [0] querySelector in this