Solved

JavaScript: Remove item from DOM created with createElement

Posted on 2012-03-18
4
506 Views
Last Modified: 2012-03-21
Using JavaScript, how can I remove an element from the DOM that I created using createElement?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Demo</title>
</head>
<body>

<h1>Hello</h1>

<script type="text/javascript">

var tmp = document.createElement("DIV");
tmp.innerHTML = 'HI <strong>WORLD</strong>';
alert( tmp.textContent||tmp.innerText );

/// Now how can I remove tmp from the DOM?

</script>

</body>
</html>

Open in new window

0
Comment
Question by:hankknight
4 Comments
 
LVL 13

Accepted Solution

by:
ansudhindra earned 250 total points
ID: 37735046
in your code its just created, but its not attached to DOM.

If it is attached to DOM say parent Div (with id as 'ptDiv') then you can do it by setting its inner html to null or using removeChild function...

cont. from your code is..

document.getElementById('ptDiv').appendChild(tmp); //appends tmp created dom to ptDiv,

then you can use ..

document.getElementById('ptDiv').removeChild(tmp)

Ref: http://dustindiaz.com/add-and-remove-html-elements-dynamically-with-javascript/
0
 
LVL 42

Assisted Solution

by:David S.
David S. earned 125 total points
ID: 37735048
You would remove it like you would any other element. It's not entirely clear what you're trying to do though since it seems "tmp" was never inserted into the DOM.
0
 
LVL 16

Author Comment

by:hankknight
ID: 37735118
I use this to strip HTML tags and process things like newlines and spaces.

So it was not attached to the DOM.  Should I remove it from where it is to save memory or just leave it?


function strip_tags (html) {
   var tmp = document.createElement("DIV");
   tmp.innerHTML = html;
   var str = tmp.textContent||tmp.innerText;
   if(!str) str = '';
   return str.replace(/(<([^>]+)>)/ig,'');
}

Open in new window

0
 
LVL 6

Assisted Solution

by:jjperezaguinaga
jjperezaguinaga earned 125 total points
ID: 37739814
Greetings hankknight,

Ahm, I think @ansudhindra is right, you can't remove what you haven't attached to the DOM. When you do with the code he gave you, you can remove it as with any DOM element: get the id with document.getElementbyID(), and then remove. (You can use jQuery too).

Checking your last post, I don't see why you are not passing directly the string to the function and replace it directly... it seems you don't need any DOM manipulation whatsoever.

Hope it helps,
-JJ
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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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