Solved

document.querySelector - how to add and remove an element

Posted on 2014-11-08
4
81 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
ID: 40430395
el.parentNode.removeChild(el);
0
 
LVL 1

Author Comment

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

Accepted Solution

by:
leakim971 earned 500 total points
ID: 40430479
if you create it, it should exist

else :

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

Author Comment

by:JElster
ID: 40430510
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how can i get the sole odd and th sole even 2 33
How to resize a div in html 3 37
Insert Button on a table 16 37
what is the best Integrated development environment 2 20
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…

856 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