JavaScript document.createElementNS() Method
You are Here:
JavaScript document.createElementNS() Method
The document.createElementNS()
method creates an element with the specified namespace URI and qualified name.
Tips: To create an element without specifying a namespace URI, use the createElement() method.
Note: The XML file used in this example is student.xml.
Example
HTML Online Editor
<!DOCTYPE html>
<html>
<body>
<p id="point"></p>
<script>
var x = document.getElementById("point");
var y, z, i, elem, text, xmlDoc, txt;
var txt = "";
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function(){
if(this.readyState == 4 && this.status == 200){
xmlDoc = xhttp.responseXML;
y = xmlDoc.getElementsByTagName("name");
for(i = 0; i < y.length; i++) {
elem = xmlDoc.createElementNS("anyString", "age");
text = xmlDoc.createTextNode("18");
elem.appendChild(text);
y[i].appendChild(elem);
}
z = xmlDoc.getElementsByTagNameNS("anyString","age");
for(i = 0; i < y.length; i++){
txt += "Name: " +y[i].childNodes[0].nodeValue ;
txt += " Age: " +z[i].childNodes[0].nodeValue ;
txt += " NS: " + z[i].namespaceURI +"<br>";
}
x.innerHTML = txt;
}
};
xhttp.open("GET", "/student.xml", true);
xhttp.send();
</script>
</body>
</html>
Syntax
document.createElementNS(namespaceURI, name)
Parameter Values
Value | Type | Explanation |
---|---|---|
namespaceURI | Required | A string that specifies the namespace URI to associate with the element. |
name | Required | A string that specifies the type of element to be created. |
Return Values
Value | Explanation |
---|---|
Object | Returns the new element. |
Reminder
Hi Developers, we almost covered 97% of JavaScript Tutorials with examples for quick and easy learning.
We are working to cover every Single Concept in JavaScript.
Please do google search for:
Join Our Channel
Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.
This channel is primarily useful for Full Stack Web Developer.