tags:

views:

29

answers:

3

Hi, I want to set id of an element. I'm using php dom. I could not understand how following will work.

DOMElement::setIdAttribute  ( string $name  , bool $isId  )

The only description I found for this in manual is - Declares the attribute name to be of type ID.

How can I do this ?

A: 

What about DOMElement::setAttribute? You could just setAttribute('id', 'your_id'), or am I missing something? After that, you could use setIdAttribute('id', TRUE) to mark it as the ID.

Reinis I.
+3  A: 

use for example:

DOMElement->setIdAttribute  ('myid', true  );
Thariama
A: 

If you want to add an id attribute to your html element so that it looks like <p id="frob">... you dont use setIdAttribute() - it declares that the attribute $name can be used as an unique identifier for that element - as an alternative/addition of the id attribute. Use setAttribute() like so:

$dom = new DOMDocument();
$dom->loadHTML('<html><body><p>FROB</p></body></html>');
$dom->getElementsByTagName('p')->item(0)->setAttribute('id', 'XXX');
print $dom->saveHTML();
Max