Solved

JavaScript: Remove item from DOM created with createElement

Posted on 2012-03-18
4
497 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
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…
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…

763 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

6 Experts available now in Live!

Get 1:1 Help Now