-3

I'm currently using jQuery to remove a div, but I'd like my project to not have to rely on a third party library for it to work. Say my div had the class .main, if I was using jQuery I'd write:

$("div.main").remove()

How would I do this with JS only?

3
  • 3
    Come on, use the search. Is it so hard? Commented Feb 27, 2016 at 11:05
  • I did use the search Commented Feb 27, 2016 at 11:07
  • 2
    No, you didn't. There are hundreds of questions with this topic on Stackoverflow and thousands of tutorials, blog posts, documentation pages and whatnot on the wider Internet. They would have been impossible to miss, if you only had tried half as long as it took you to write down this question. Commented Feb 27, 2016 at 11:14

2 Answers 2

5

Duplicate question but try this: (Set an id for it)

var elem = document.getElementById("myDiv");
elem.parentNode.removeChild(elem);

or take a look at this topic:

Remove all elements of a certain class with JavaScript

Sign up to request clarification or add additional context in comments.

3 Comments

Doesn't .remove(); Require jQuery? No it didn't work.
I'm lost at the parentNode bit, sorry @jiff
@Verpz see this example JSFiddle
0

Something like this :

el.parentNode.removeChild(el);

IE8+

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.