Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 109
  • Last Modified:

document.querySelector - how to add and remove an element

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
JElster
Asked:
JElster
  • 2
  • 2
1 Solution
 
leakim971PluritechnicianCommented:
el.parentNode.removeChild(el);
0
 
JElsterAuthor Commented:
How do I check if it exists and then remove it.
0
 
leakim971PluritechnicianCommented:
if you create it, it should exist

else :

if(el) el.parentNode.removeChild(el);
0
 
JElsterAuthor Commented:
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

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now