M.J.Ahmadi
M.J.Ahmadi

Reputation: 3931

DomDocument innerHTML does not work in simple php script

innerHTML in php DomDocument does not work in simple PHP script and I can't set the inner html of html element by id, There is no error/warning! what should I do ?

(I also checked the file name and the html element id)

    $index = new DomDocument;
    $index->validateOnParse = true;
    $index->formatOutput = true;


    $index->loadHTML('index.php');

    $index->getElementById('element-unique-id')->innerHTML = 'some text';


    echo $index->saveHTML();

the out put is blank.

Upvotes: 0

Views: 340

Answers (1)

insertusernamehere
insertusernamehere

Reputation: 23600

As there's no inneHTML-property, you can solve it by creating and appending a new DOMText node:

$index->getElementById('element-unique-id')
      ->appendChild(new DOMText('some text'));

See also The DOMText class and DOMText::__construct.

Upvotes: 2

Related Questions