473,385 Members | 2,014 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

removeChild and appendChild for Span

I need to clear out the HTML contents of a <span> element and then
re-add that element as a blank span.

Here is the code...

var elem = document.getElementById(spanId);

var deletedElem = elem.parentNode.removeChild(elem);
deletedElem.innerHTML = '<span id="'+spanId+'"></span>';
deletedElem.parentNode.appendChild(deletedElem);

This does not work. When I try to getElementById the second time the
function is called it does not find the span I added. Any suggestions?

Thanks in advance for any help.

Jul 23 '05 #1
1 3673
JehanN...@aol.com wrote:
I need to clear out the HTML contents of a <span> element and then
re-add that element as a blank span.

Here is the code...

var elem = document.getElementById(spanId);

var deletedElem = elem.parentNode.removeChild(elem);
deletedElem.innerHTML = '<span id="'+spanId+'"></span>';
deletedElem.parentNode.appendChild(deletedElem);

This does not work. When I try to getElementById the second time the
function is called it does not find the span I added. Any suggestions?
Thanks in advance for any help.


You're removing the child span:

var deletedElem = elem.parentNode.removeChild(elem);

....effectively 'detatching' it from the document; then...

deletedElem.parentNode.appendChild(...

....referencing its parent node for appending. No parent node to
reference.

If you just want to clear out the element's descendents, try -

var elem;
if (elem = document.getElementById(spanId))
{
while (elem.hasChildNodes())
elem.removeChild(elem.lastChild);
}

Should clean it up nicely.

Jul 23 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: samuel.adam | last post by:
Hi all, I am coding an AJAX DHTML whatever application and I was fed up with always typing a lot of appendChild() functions. I created a custom one called append_children() and wanted to share...
2
by: Stewart | last post by:
Originally posted in comp.lang.javascript: Newsgroups: comp.lang.javascript From: "Stewart" Date: 23 Aug 2005 02:50:04 -0700 Local: Tues, Aug 23 2005 10:50 am Subject: FireFox, RemoveChild,...
3
by: sfeher | last post by:
Hi All, I'm creating a table using createElement / appendChild and everything works great. Ussualy I have no more than 20 rows and they contain less that 5 columns. Where my code starts to...
8
by: Satanduvel | last post by:
I have a script that addElements but also an script that delete the added element. In FF it works but in I.E it doesn't. This is the code <script type="text/javascript"> function...
4
by: joecap5 | last post by:
I have a main window and a menu window. The menu window is opened from the main window by clicking on "Open Menu". A google window can also be opened from the main window by clicking "Open Google"....
5
by: tader | last post by:
Hi, so i got script like that var div_box = document.createElement("div"); div_box.setAttribute("id", "music_box"); div_box.style.top = (Number(cords) - 10) + "px"; div_box.style.left =...
10
by: r_ahimsa_m | last post by:
Hello, On index.html page I have a table with id="property_fields". <table id="property_fields" name="property_fields" border="0"> It contains set of rows with the following IDs: var...
5
by: r_ahimsa_m | last post by:
Hello, I am lerning HTML/CSS/JavaScript. I created HTML page with table "property_fields" containing 24 rows ('tr' elements). I want to remove last 23 rows: var table =...
7
by: ChuckB via WebmasterKB.com | last post by:
Hello. I'm trying to remove an additional radio selection if another radio button is selected. There's suppose to be two additional selections if one particular button is chosen. Then if the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.