JS hasAttribute(

The hasAttribute() method returns true if the attribute exists, otherwise false.

Syntax

element.hasAttribute(name)

Parameters

Parameter Description
name Required.
The name of the attribute.

Return Value

Type Description
Boolean true if the element has the attribute, otherwise false.

hasChildNodes(

The hasChildNodes() method returns true if the specified node has any child nodes, otherwise false.

The hasChildNodes() method is read-only.

Syntax

element.hasChildNodes()
 

Parameters

NONE

Return Value

Type Description
Boolean true if the element has child nodes, otherwise false.

html file

<!DOCTYPE html>
<html>
<head>
    <title>DOM Navigation</title>
    <style>
        h2{ margin: 0 0 10px; }
        #test{
            background: #ffff00;
            width: 800px;
            min-height: 250px;
            padding: 10px 10px;
            margin: 0 auto;
        }
    </style>
</head>
<body>
    <div id="test" class="abc">
        <h2>Yahoo Baba : JavaScript hasAttribute Method</h2>
        <p>
            Lorem ipsum dolor sit amet, consectetur adipisicing elit. Explicabo corporis adipisci alias cupiditate officiis consequatur, fuga, amet quos numquam perferendis saepe labore dolorem reiciendis reprehenderit facilis repudiandae praesentium quis delectus voluptates, repellendus recusandae. Itaque consequuntur, corrupti quasi illum iusto perferendis autem blanditiis magni eum, repellendus architecto ratione ipsum molestiae laboriosam.
        </p>
         <h2>Yahoo Baba : JavaScript hasChildNodes Method</h2>
    </div>
    <script src="js/dom-create.js"></script>
</body>
</html>


dom-create.js

/* JavaScript hasAttribute*/

var target  = document.getElementById("test");

var find = target.hasAttribute("class");

console.log(find);


/* JavaScript hasChildNodes*/

var target  = document.getElementById("test");

var find = target.hasChildNodes();

console.log(find);