How to use DOM Element SetAttributeNodeNs in JavaScript

In this article I am going to explain about DOM element setAttributeNodeNS() method in JavaScript.
  • 1656

JavaScript DOM Element setAttributeNodeNS() Method

JavaScript setAttributeNodeNS() method use for set specified attribute node to an element in specified namespace. If attribute already exists on element then then its will replaced it.

Syntax

element.setAttributeNodeNS(Url,attributeNode)

Example

<html>

<head>

    <script type="text/javascript">

        var http;

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

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

 

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

        yy.send();

        var xmlDoc = yy.responseXML;

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

 

        function check() {

            var y = x.hasAttributeNS("https://dotnetheaven.com/NS", "country");

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

        };

 

        function Add() {

            var mm = xmlDoc.createAttributeNS("https://dotnetheaven.com/NS", "country");

            mm.value = "India";

            x.setAttributeNodeNS(mm);

        };

    </script>

</head>

<body>

    <button onclick="check()">

        check it</button>

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

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

    <p>

        click this button to add country attribute</p>

    <button onclick="Add()">

        Add attribute</button>

</body>

</html>

 

Output

 

Before click

 

 AddNs1.jpg

 

After click

 

 AddNs2.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

© 2020 DotNetHeaven. All rights reserved.