Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

replaceChild() and Keeping Relationships

Status
Not open for further replies.

AbidingDude

Programmer
Oct 7, 2012
74
US
I'm working on a form. The form is a table with pieces of equipment in one column, and the readings from that equipment in another column (plus a few others).
The equipment ID's are hard-coded into the form. Sometimes one of these pieces of equipment malfunctions or has to go out for repair, so we have to use a substitute piece of equipment.
I'm trying to make the form flexible. When the user clicks on a "Change Equipment" button, it adds check boxes to the bottom of the IDs. When a box is checked, I change the text node of the ID to a text entry box. To do that I've been using replaceChild().
After the swap I need to access some of the parent and sibling nodes that surround it. It's been giving me errors. Apparently, when I replace a node, it does not take on the same parent/sibling elements as the original. When I examine the DOM elements of it, it says NULL for all of them.
I tried assigning to parentNode and nextSibling, etc, but they are apparently read-only. How can I get a replacement node to inherit the parents and siblings of the original?

WIP code:
JavaScript:
function handle_checkbox(check_in)
{
	var ch;
	var in_swap=document.createElement("INPUT");

	in_swap.setAttribute("type","text");
	in_swap.setAttribute("size","12");
	in_swap.setAttribute("maxlength","15");
	
	ch=check_in.parentNode; /*Parent node is the <TD>*/
	if(check_in.checked==true){
		/*Swap the ID # text node with an input field.*/
		ch.replaceChild(in_swap,ch.childNodes[0]);
		ch.childNodes[0].focus();
	}
	else{
		putback(ch.childNodes[0]);
		console.log(ch.childNodes[0]);
	}
}
 
Nevermind. I'm an idiot. The relationships are kept. I was just accidentally checking the replaced node instead of the new one.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top