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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

InsertBefore method

Status
Not open for further replies.

lbrooks

IS-IT--Management
Jan 20, 2004
42
0
0
US
Overall: When I attempt to use the insertBefore method I get an error "Insert position node must be a child of the node to insert under." Below I walk through my code explaining what I am doing. Any help would be grealy appreciated.


//this is just to show that I am working with two different XMl files. One to clone a node from and the other to actually insert into.

objBatchDoc = objBatch.documentElement;
objBatchTemplateDoc = objTemplateBatch.documentElement;


//Here I am a getting a node from another file with the same structure and then cloning it. It will be used as the new node I need to insert

temp = "MasterRecipe/RecipeElement";
var objMasterMainNode = objBatchTemplateDoc.selectSingleNode(temp);
if (objMasterMainNode == null) {
alert('object is nul');

} else {
var objHoldingNode = objMasterMainNode.cloneNode(true);


//end getting new node, next I am setting some initial values for this cloned node.

objHoldingNode.selectSingleNode('ID').text = ElementName;
objHoldingNode.selectSingleNode('Description').text = ElementDesc;


//next I point to the node where I want the new node to be inserted above and it does pull back a node.

temp = "MasterRecipe[ID='" + RecipeID + "']/RecipeElement/RecipeElement/RecipeElement[ID='" + CurrentNodeID + "']";
var objBatchCurrent = objBatchDoc.selectSingleNode(temp);


//next I want to get the parent of the node above to be used in the insertBefore function.
var temp2 = "MasterRecipe[ID='" + RecipeID + "']/RecipeElement/RecipeElement";

var objBatchCurrentPlusOne;
objBatchCurrentPlusOne = objBatchDoc.selectSingleNode(temp2);


//next I attempt to insert the node. But I get an error "Insert position node must be a child of the node to insert under" on the insertBefore Function. Please help I can't seem to figure what I am doing wrong.

if (objBatchCurrentPlusOne == null) {

}else{


objBatchCurrentPlusOne.insertBefore(objHoldingNode, objBatchCurrent);

}
 
Is my problem clear, do I need to provide more?
 
Do it like this which may be more orderly.
[tt]
// etc etc... (above same)

[green]//put it lower and define it differently[/green]
//next I point to the node where I want the new node to be inserted above and it does pull back a node.

[red]//[/red] temp = "MasterRecipe[ID='" + RecipeID + "']/RecipeElement/RecipeElement/RecipeElement[ID='" + CurrentNodeID + "']";
[red]//[/red] var objBatchCurrent = objBatchDoc.selectSingleNode(temp);

//next I want to get the parent of the node above to be used in the insertBefore function.
var temp2 = "MasterRecipe[ID='" + RecipeID + "']/RecipeElement/RecipeElement";

var objBatchCurrentPlusOne;
objBatchCurrentPlusOne = objBatchDoc.selectSingleNode(temp2);

[green]//define the current here[/green]
//next I point to the node where I want the new node to be inserted above and it does pull back a node.
[green] temp = "RecipeElement[ID='" + CurrentNodeID + "']";
var objBatchCurrent = objCurrentPlusOne.selectSingleNode(temp);[/green]

//next I attempt to insert the node. But I get an error "Insert position node must be a child of the node to insert under" on the insertBefore Function. Please help I can't seem to figure what I am doing wrong.

if (objBatchCurrentPlusOne == null) {

}else{

objBatchCurrentPlusOne.insertBefore(objHoldingNode, objBatchCurrent);
}
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top