How to use DOM Element HasAttribute in JavaScript

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

JavaScript DOM Element hasAttribute() Method

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

Syntax

element.hasAttribute(attribute name)

Example

<html>

<head>

    <script type="text/javascript">

 

        function myfun() {

            var mm = document.getElementsByTagName("BUTTON")[0];

            var pp = document.getElementById("tst");

            pp.innerHTML = mm.hasAttribute("onclick");

        };

 

    </script>

</head>

<body>

    <p id="tst">

        click button for check element exists or not exists</p>

    <button onclick="myfun()">

        click me</button>

</body>

</html>

 

Output

 

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

© 2019 DotNetHeaven. All rights reserved.