PHP | DOMElement removeAttribute() Function Last Updated : 26 Feb, 2020 Comments Improve Suggest changes Like Article Like Report The DOMElement::removeAttribute() function is an inbuilt function in PHP which is used to remove a attribute with specific name from the element. Syntax: bool DOMElement::removeAttribute( string $name ) Parameters: This function accepts a single parameter $name which holds the name of the attribute. Return Value: This function returns TRUE on success or FALSE on failure. Exceptions: This function throws DOM_NO_MODIFICATION_ALLOWED_ERR, if the node is read-only. Below examples illustrate the DOMElement::removeAttribute() function in PHP: Example 1: php <?php // Create a new DOMDocument $dom = new DOMDocument(); // Load the XML $dom->loadXML("<?xml version=\"1.0\"?> <root> <html> <h1 id=\"my_id\"> Geeksforgeeks </h1> <h2> Second heading </h2> </html> </root>"); // Get the elements $node = $dom->getElementsByTagName('h1')[0]; echo "Before the removal of attributes: <br>"; // Get the attribute name and value $attribute = $node->attributes->item(0); $attribute_name = $attribute->name; $attribute_value = $attribute->value; echo $attribute_name . ' => '; echo $attribute_value; // Remove the id attribute $node->removeAttribute('id'); echo "<br>After the removal of attributes: <br>"; // Get the attribute name and value $attribute = $node->attributes->item(0); $attribute_name = $attribute->name . ' => '; $attribute_value = $attribute->value; echo $attribute_name; echo $attribute_value; ?> Output: Before the removal of attributes: id => my_id After the removal of attributes: => // Empty value means attribute is removed Example 2: php <?php // Create a new DOMDocument $dom = new DOMDocument(); // Load the XML $dom->loadXML("<?xml version=\"1.0\"?> <root> <html> <h1 id=\"my_id\" style=\"color:green;\" class=\"my_class\"> Geeksforgeeks </h1> <h2> Second heading </h2> </html> </root>"); // Get the elements $node = $dom->getElementsByTagName('h1')[0]; echo "Before the removal of attributes: <br>"; // Get the attribute count $attributeCount = $node->attributes->count(); echo 'No of attributes => ' . $attributeCount; // Remove the id attribute $node->removeAttribute('id'); echo "<br>After the removal of attributes: <br>"; // Get the attribute count $attributeCount = $node->attributes->count(); echo 'No of attributes => ' . $attributeCount; ?> Output: Before the removal of attributes: No of attributes => 3 After the removal of attributes: No of attributes => 2 Reference: https://www.php.net/manual/en/domelement.removeattribute.php Comment More infoAdvertise with us Next Article PHP | DOMElement removeAttribute() Function G gurrrung Follow Improve Article Tags : Web Technologies PHP PHP-function PHP-DOM Similar Reads PHP | DOMElement removeAttributeNS() Function The DOMElement::removeAttributeNS() function is an inbuilt function in PHP which is used to remove the attribute with a specific local name in a specific namespace from the element. Syntax: bool DOMElement::removeAttributeNS( string $namespaceURI, string $localName ) Parameters: This function accept 2 min read PHP | DOMElement removeAttributeNode() Function The DOMElement::removeAttributeNode() function is an inbuilt function in PHP which is used to remove attribute from element. Syntax: bool DOMElement::removeAttributeNode( DOMAttr $oldnode ) Parameters: This function accepts a single parameter $oldnode which holds the attribute that to be removed. Re 2 min read PHP | DOMElement setAttribute() Function The DOMElement::setAttribute() function is an inbuilt function in PHP which is used to set an attribute with given name to the given value. If the attribute does not exist, it will be created. Syntax: DOMAttr DOMElement::setAttribute( string $name, string $value ) Parameters: This function accepts t 2 min read PHP | DOMElement setAttributeNS() Function The DOMElement::setAttributeNS() function is an inbuilt function in PHP which is used to set an attribute with given namespace and name to the given value. If the attribute does not exist, it will be created. Syntax: void DOMElement::setAttributeNS( string $namespaceURI, string $qualifiedName, strin 2 min read PHP | DOMElement setIdAttribute() Function 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 bel 2 min read Like