Solved

JavaScript: Remove item from DOM created with createElement

Posted on 2012-03-18
4
509 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

724 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