How to use DOM Element hasAttribute in JavaScript

In this article I am going to explain about DOM Element hasAttribute() method in JavaScript.
  • 1363

JavaScript DOM Element hasAttributeNS() Method

JavaScript hasAttribute() method use for check specified attribute exists or not exists in specified namespace . If attribute exists then it will return true other wise false.

Syntax

element.hasAttributeNS(attribute name)

Example

<html>

<head>

    <script type="text/javascript">

        function myfun() {

            var http;

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

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

 

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

            Xm.send();

            var xmlDoc = Xm.responseXML;

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

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

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

        };

    </script>

</head>

<body>

    <p id="tst">

        click button for check exist or not exist</p>

    <button onclick="myfun()">

        click me</button>

</body>

</html>

 

Output

 

 hsns.gif

 

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.