JavaScript - createAttributeNS() method

revision:


Category : document

The Document.createAttributeNS() method creates a new attribute node with the specified namespace URI and qualified name, and returns it. The object created is a node implementing the Attr interface. The DOM does not enforce what sort of attributes can be added to a particular element in this manner.

Syntax :

        createAttributeNS(namespaceURI, qualifiedName)
    

Parameters:

namespaceURI : a string that specifies the namespace URI to associate with the attribute. The namespaceURI property of the created attribute is initialized with the value of namespaceURI.

qualifiedName : a string that specifies the name of attribute to be created. The name property of the created attribute is initialized with the value of qualifiedName.

Examples:

            const node = document.getElementById("svg");
            const a = document.createAttributeNS("http://www.w3.org/2000/svg", "viewBox");
            a.value = "0 0 100 100";
            node.setAttributeNode(a);
            console.log(node.getAttribute("viewBox")); // "0 0 100 100"    
        

Practical examples

example:
code:
                    
                

example:
code:
                    
                

example:
code: