Solved

document.querySelector - how to add and remove an element

Posted on 2014-11-08
4
75 Views
Last Modified: 2014-11-10
Hi..

I can add an element using the following

              var rect = document.createElement('div');

              var el = document.querySelector('.results').appendChild(rect);


How can I then remove it ?
0
Comment
Question by:JElster
  • 2
  • 2
4 Comments
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
el.parentNode.removeChild(el);
0
 
LVL 1

Author Comment

by:JElster
Comment Utility
How do I check if it exists and then remove it.
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
Comment Utility
if you create it, it should exist

else :

if(el) el.parentNode.removeChild(el);
0
 
LVL 1

Author Comment

by:JElster
Comment Utility
How can I check the document, before I add it so I don't add it again.
This code will be in a click event
thx
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now