?
Solved

JavaScript: Remove item from DOM created with createElement

Posted on 2012-03-18
4
Medium Priority
?
513 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 1000 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 43

Assisted Solution

by:David S.
David S. earned 500 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 500 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…
Suggested Courses

864 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