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.


element.hasAttributeNS(attribute name)




    <script type="text/javascript">

        function myfun() {

            var http;

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

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


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


            var xmlDoc = Xm.responseXML;

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

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

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





    <p id="tst">

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

    <button onclick="myfun()">

        click me</button>








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.