Jump to content

Replace Node Problem


chibineku
 Share

Recommended Posts

I am trying to create a simple script that will find an element with a certain id, and swap it for a different id. Doing so directly doesn't work, so I am trying to replicate the node with that id and swap it for a node with the same content and my new id. I have no problem with the new node creation, but the replaceNode method doesn't seem to be having the desired effect.The whole idea is to have PHP tell me the name of the current page, and each of my nav menu links (style liked tabs) has an id matching the name of the page. I use JavaScript to extract just the bit of $_SERVER["PHP_SELF"] that I need and use that as the id of the element.Here is the script (once PHP has been evaluated):

function setCurPage() {var CurPage = "/contact.php"; var CurRaw = CurPage.substr(1);var Cur = CurRaw.split(".")[0];var newNode = document.createElement("li");newNode.id = "cur";var innards = document.getElementById(Cur).children;while(innards.length > 0) {newNode.insertBefore(innards[0]);}var oldNode = document.getElementById(Cur).replaceNode(newNode);}

The element in question is a list item with a link.Any ideas why replaceNode isn't working?

Link to comment
Share on other sites

I'm not exactly aware of a replaceNode() method. I do know of replaceChild() though.You can use it like:

var oldNode = document.getElementById(Cur);oldNode.parentNode.replaceChild(newNode, oldNode);

Edited by boen_robot
Link to comment
Share on other sites

I'm not exactly aware of a replaceNode() method. I do know of replaceChild() though.You can use it like:
var oldNode = document.getElementById(Cur);oldNode.parentNode.replaceChild(newNode, oldNode);

Hm, odd...my JavaScript Bible has replaceNode and I'm using it verbatim. I tried your way too, and it isn't working either - the alert that I put in after the swap to display the new element's outerHTML also stopped working. Gotta go out, will be back in 30 mins to try more.
Link to comment
Share on other sites

Just to make certain - your alert is something like:

alert(document.getElementById("cur").outerHTML);

and you are testing in IE, right?

Edited by boen_robot
Link to comment
Share on other sites

Just to make certain - your alert is something like:
alert(document.getElementById("cur").outerHTML);

and you are testing in IE, right?

I will try again, but that's pretty much what I had...it worked for a while, before I made some other changes. I am testing in FF.
Link to comment
Share on other sites

My alerts work BEFORE I try the replaceChild method, but not after. Strange.This is the new code, BTW:

function setCurPage() {  var CurPage = "<?php echo $_SERVER["PHP_SELF"]; ?>"; // equates to "/contact.php"  var CurRaw = CurPage.substr(1);  var Cur = CurRaw.split(".")[0];  var oldNode = document.getElementById(Cur);var newNode = document.createElement("li");newNode.id = "cur";var innards = document.getElementById(Cur).children;while(innards.length > 0) {newNode.insertBefore(innards[0]);}oldNode.parentNode.replaceChild(newNode, oldNode);}

edit: i am getting an error in FireFox error console:Error: uncaught exception: [Exception... "Not enough arguments" nsresult: "0x80570001 (NS_ERROR_XPC_NOT_ENOUGH_ARGS)" location: "JS frame :: http://www.sinaesthesia.co.uk/contact.php :: setCurPage :: line 18" data: no]I don't believe that needs more arguments...

Edited by chibineku
Link to comment
Share on other sites

AFAIK, outerHTML is an IE property that's supported in IE... not in Firefox. That's why it may not be working. That's why I asked you if you're testing in IE.As for the error (which I've only noticed now, like you), it occurs because when an element has no child nodes, insertBefore() is invalid. You need to use appendChild() instead at that first point.

Edited by boen_robot
Link to comment
Share on other sites

Ah ha! It worketh!Many thanks: truly do you both dom the DOM.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...