You are here: Reference > JavaScript > client-side > HTML DOM > methods > createElementNS (document, XMLDocument)

createElementNS method (document, XMLDocument)

Browser support:
9
Creates an element with the specified namespace prefix and name.
Note: Internet Explorer supports the createElementNS method from version 9, but only for HTML documents, not for XML documents.
After the element is created, use the appendChild or insertBefore method to insert it into a document.
If you do not want to specify a namespace for the newly created element, use the createElement method instead.

Syntax:

object.createElementNS (namespaceURI, name);
You can find the related objects in the Supported by objects section below.

Parameters:

namespaceURI
Required. String that specifies the namespace URI of the element to be created. The namespaceURI property of the created element is set to this value.
name
Required. String that specifies the name of the element to be created. The nodeName property of the created element is set to this value.

Return value:

Returns the newly created element.

Example HTML code 1:

This example illustrates the use of the createElementNS method:
Code
ajax.js
ns.xml
<head>
    <script type="text/javascript" src="ajax.js"></script>
    
    <script type="text/javascript">
        var httpRequest = null;
        
        function SendRequest () {
            if (!httpRequest) {
                httpRequest = CreateHTTPRequestObject ();   // defined in ajax.js
            }
            if (httpRequest) {          
                    // The requested file must be in the same domain that the page is served from.
                var url = "ns.xml";
                httpRequest.open ("GET", url, true);    // async
                httpRequest.onreadystatechange = OnStateChange;
                httpRequest.send (null);
            }
        }

        function OnStateChange () {
            if (httpRequest.readyState == 0 || httpRequest.readyState == 4) {
                if (IsRequestSuccessful (httpRequest)) {    // defined in ajax.js
                    Test_CreateAttributeNS ();
                }
                else {
                    alert ("Operation failed.");
                }
            }
        }

        function Test_CreateAttributeNS () {
            var xmlDoc = ParseHTTPResponse (httpRequest);   // defined in ajax.js
            if (!xmlDoc)
                return;

            var itemTags = xmlDoc.getElementsByTagName ("item");
            var firstItem = itemTags[0];

            if (xmlDoc.createElementNS) {
                var placeTag = xmlDoc.createElementNS ("http://help.dottoro.com/NS", "place");
                var placeText = xmlDoc.createTextNode ("Alabama");
                placeTag.appendChild (placeText);
                firstItem.appendChild (placeTag);
                
                var serializer = new XMLSerializer();
                alert(serializer.serializeToString(firstItem));
            }
            else {
                alert ("Your browser does not support this example!");
            }
        }

    </script>
</head>
<body>
    <button onclick="SendRequest ()">Test the createAttributeNS method</button>
</body>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content