Solved

JavaScript: Remove item from DOM created with createElement

Posted on 2012-03-18
4
505 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
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…

809 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