PHP | DOMElement setIdAttributeNode() Function
Last Updated :
26 Feb, 2020
The DOMElement::setIdAttributeNode() function is an inbuilt function in PHP which is used to declare the attribute specified by DOMAttr instance to be of type ID.
Syntax:
void DOMElement::setIdAttributeNode( DOMAttr $attr, bool $isId )
Parameters: This function accepts two parameters as mentioned above and described below:
- $attr: It specifies the attribute as a DOMAttr instance.
- $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::setIdAttributeNode() function in PHP:
Example 1:
<?php
$dom = new DOMDocument( '1.0' , 'iso-8859-1' );
$dom ->validateOnParse = true;
$element = $dom ->appendChild( new DOMElement
( 'div' , 'GEEKSFORGEEKS' ));
$id = new DOMAttr( 'id' , 'geeksforgeeks' );
$attr = $element ->setAttributeNode( $id );
$element ->setIDAttributeNode( $id , true);
echo $dom ->saveXML();
?>
|
Output:
Example 2:
<?php
$dom = new DOMDocument( '1.0' , 'iso-8859-1' );
$dom ->validateOnParse = true;
$element = $dom ->appendChild( new DOMElement
( 'div' , 'Hey ! This is my content.' ));
$id = new DOMAttr( 'id' , 'geeksforgeeks' );
$attr = $element ->setAttributeNode( $id );
$element ->setIDAttributeNode( $id , true);
$value = $dom ->getElementById( 'geeksforgeeks' )->textContent;
echo $value ;
?>
|
Output:
Hey ! This is my content.
Reference: https://www.php.net/manual/en/domelement.setidattributenode.php
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...