PHP | DOMElement setIdAttribute() Function
Last Updated :
26 Feb, 2020
The DOMElement::setIdAttribute() function is an inbuilt function in PHP which is used to declare the attribute specified by name to be of type ID.
Syntax:
void DOMElement::setIdAttribute( string $name, bool $isId )
Parameters: This function accepts two parameters as mentioned above and described below:
- $name: It specifies the name of the attribute.
- $isId: It specifies whether if you want name to be of type ID.
Return Value: This function returns nothing.
Exceptions: This function throws DOM_NO_MODIFICATION_ALLOWED_ERR, if the node is readonly or DOM_NOT_FOUND, if name is not an attribute of this element.
Below examples illustrate the DOMElement::setIdAttribute() function in PHP:
Example 1:
<?php
$dom = new DOMDocument( '1.0' , 'iso-8859-1' );
$dom ->validateOnParse = true;
$element = $dom ->appendChild( new DOMElement( 'div' ));
$attr = $element ->setAttributeNode( new DOMAttr( 'id' ,
'geeksforgeeks' ));
$element ->setIDAttribute( 'id' , true);
echo $dom ->saveXML();
?>
|
Output: Press Ctrl+U to see the DOM.
Example 2:
<?php
$dom = new DOMDocument( '1.0' , 'iso-8859-1' );
$dom ->validateOnParse = true;
$element = $dom ->appendChild( new DOMElement( 'div' ,
'GEEKSFORGEEKS' ));
$attr = $element ->setAttributeNode( new DOMAttr( 'id' ,
'geeksforgeeks' ));
$element ->setIDAttribute( 'id' , true);
$value = $dom ->getElementById( 'geeksforgeeks' )->textContent;
echo $value ;
?>
|
Output:
GEEKSFORGEEKS
Reference: https://www.php.net/manual/en/domelement.setidattribute.php
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...