JavaScript - getAttributeNS() method

revision:


Category : element

The getAttributeNS() method of the Element interface returns the string value of the attribute with the specified namespace and name. If the named attribute does not exist, the value returned will either be "null" or "" (the empty string).

Syntax :

        getAttributeNS(namespace, name)
    

Parameters:

namespace : the namespace in which to look for the specified attribute.

name : the name of the attribute to look for.

Examples:

            <svg
                xmlns="http://www.w3.org/2000/svg"
                xmlns:test="http://www.example.com/2014/test"
                width="40"
                height="40">
                <circle
                id="target"
                cx="12"
                cy="12"
                r="10"
                stroke="#444"
                stroke-width="2"
                fill="none"
                test:foo="Foo value" />
            </svg>
        
            <script>
                const ns = "http://www.example.com/2014/test";
                const circle = document.getElementById("target");
                console.log(`Attribute value: ${circle.getAttribute("test:foo")}`);
            </script>
        

Practical examples

example:
code:
                    
                

example:
code:
                    
                

example:
code: