I use the following to get the html document in the DOM:
$dom = new domDocument('1.0', 'utf-8');
$dom->loadHTML($html)
and then add new content to the element in html:
$element = $dom->getElementById('mybox');
$f = $dom->createDocumentFragment();
$f->appendXML('<div id="newbox">foo</div>');
$element->appendChild($f);
But if now I want to manipulate #newbox, I cannot do this because I cannot access it using getElementById(). To do this, I need to do the following (reboot with the new html):
$html = $dom->saveHTML();
$dom->loadHTML($html)
Which works well, but when you have to do this between each dom manipulator, it becomes expensive in performance.
Is there a better way to “update” the DOM so that it works with newly added elements?
Thanks in advance!:)
source
share