473,594 Members | 2,682 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.getEle mentById(spanId );

var deletedElem = elem.parentNode .removeChild(el em);
deletedElem.inn erHTML = '<span id="'+spanId+'" ></span>';
deletedElem.par entNode.appendC hild(deletedEle m);

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 3686
JehanN...@aol.c om 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.getEle mentById(spanId );

var deletedElem = elem.parentNode .removeChild(el em);
deletedElem.inn erHTML = '<span id="'+spanId+'" ></span>';
deletedElem.par entNode.appendC hild(deletedEle m);

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(el em);

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

deletedElem.par entNode.appendC hild(...

....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.getEle mentById(spanId ))
{
while (elem.hasChildN odes())
elem.removeChil d(elem.lastChil d);
}

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
10844
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 it if anyone need such a function. function append_children() { var a = append_children.arguments;
2
1450
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, AppendChild, making width grow? Hi,
3
9856
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 break (IE6 only, FF works great) is when I quickly clear(removeChild) the table and insert new rows. My problem is that I am getting a IE6 crash and the last log message shows a
8
2217
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 addAttachmentElement() { var container = document.getElementById('attachment_container'); var counter = document.getElementById('attachment_counter').value;
4
2359
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". If "google_win" is clicked on in the menu window the google window is focused on. Now if "Add Google to Menu" is clicked in the main window, another line is written to the menu window also saying "google_win". This second "google_win" is added...
5
5553
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 = (Number(cords) - 10) + "px"; document.body.appendChild(div_box);
10
3199
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 propertyRows = new Array( "region_row", "town_row", "district_row", "address_row", "house_row", "floor_no_row", "rooms_row", "floors_row", "area_row", "year_built_row", "ownership_row", "standard_row", "garrage_row", "parking_row", "kitchen_row",...
5
6347
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 = document.getElementById("property_fields"); var tbody = table.tBodies; for (var r = 1, len = tbody.rows.length; r < len; r++) tbody.removeChild(tbody.lastChild);
7
3752
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 user picks another button then this additional selection disappears. I've been trying the removeChild method but it doesn't seem to be working. The code is part of a larger form code, so I'll just post the problem area...
0
7954
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7882
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8259
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8377
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8016
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6669
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5415
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3905
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1487
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.