tags:

views:

18

answers:

1

Hi, I've got the following:

http://pastie.org/1066861

I've got a Javascript function, that tells me to delete a given node from the parent, let's assume position 1 ( from 0 or 1, since we've got two child nodes on parent ), When doing $parent->getElementsByTagName and trying to remove the the $child->item( 1 ), I notice that PHP GetElementsByTagName is giving me a diferent position, for that specific node. So, there's no way to access the right node, that JS told.

I guess I should stay away of getElementsByTagName, and use getElementsById, but right now I would like to learn and understand why this doesnt work properly.

Here's some code: http://pastie.org/1066857

Thanks a lot for your atention ;D

+1  A: 

Found a solution,

/*
 * deleteNode() removes given node, in position X.
 */
function deleteNode( $html, $strParent, $intNodePosition ) {

    $domhtml = DOMDocument::loadHTML($html); 

    $xpath = new DOMXPath($domhtml);

    $divs = $xpath->query( "*/div[@id='maindiv']/div" );

    $node = $divs->item( $intNodePosition );

    $node->parentNode->removeChild( $node );

/* this wont work, but why ? It should $divs->removeChild( $node ); */

return $domhtml->saveHTML();

}//fim deleteNode
On DOMXpath, why is this valid $nodechildren->parentNode->removeChild( $nodechildren ); and not this $parent->removeChild( $nodechildren ); If I'm access the parent and telling him to remove Is child, why do I need to access the child call the parent of this child and tell him to remove himself from it ?
`$divs` in your code is a `DOMNodeList` which is a collection of nodes. `$node->parentNode` is a `DOMElement` (usually) which is a single node. So, you're not accessing "the parent" when you think you are (with the `DOMNodeList`).
salathe