How to use DOM Element SetAttributeNS in JavaScript

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

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.

Syntax

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

Example

<html>

<head>

    <script type="text/javascript">

        var http;

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

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

 

        xhttp.open("GET", "myfile.xml", false);

        xhttp.send();

        var xmlDoc = xhttp.responseXML;

        var x = xmlDoc.getElementsByTagNameNS("http://www.dotnetheaven.com", "author")[0];

 

        function check() {

            var y = x.hasAttributeNS("http://www.dotnetheaven.com", "Country");

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

        };

 

        function Add() {

            x.setAttributeNS("http://www.dotnetheaven.com", "Country", "India");

        };

    </script>

</head>

<body>

    <button onclick="check()">

        check</button>

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

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

    <p>

        click this button to add attribute</p>

    <button onclick="Add()">

        Add attribute</button>

</body>

</html>

 

Output

 

Before click

 

 SetAttrNS1.jpg

 

After click

 

 SetAttriNS2.jpg

 

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
 

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.