How to use DOM Element SetAttributeNS in JavaScript

In this article I am going to explain about DOM element setAttributeNS() method in JavaScript
  • 2238

JavaScript DOM Element setAttributeNS() Method

JavaScript setAttributeNS() method use for set specified attribute instead of old attribute and give specified value in specefied namespace. If both attribute are same then only value will be change.


element.setAttributeNS(nameSpace url, attribute name, attribute value)




    <script type="text/javascript">

        var http;

        if (window.XMLHttpRequest) { xhttp = new XMLHttpRequest(); }

        else { xhttp = new ActiveXObject("Microsoft.XMLHTTP"); }

 "GET", "myfile.xml", false);


        var xmlDoc = xhttp.responseXML;

        var x = xmlDoc.getElementsByTagNameNS("", "author")[0];


        function check() {

            var y = x.hasAttributeNS("", "Country");

            document.getElementById("tst").innerHTML = y;



        function Add() {

            x.setAttributeNS("", "Country", "India");





    <button onclick="check()">


    <h1 id="tst" style="color: Blue">

        click button to check attribute exists or not</h1>


        click this button to add attribute</p>

    <button onclick="Add()">

        Add attribute</button>






Before click




After click




You may also want to read these related articles: here
Ask Your Question 
Got a programming related question? You may want to post your question here


More Articles

© 2020 DotNetHeaven. All rights reserved.