Sunday 30 September 2018

JavaScript: getAttribute: get the value associated with the attribute

getAttribute method is used to get the value associated with the attribute.

Syntax
element.getAttribute(attributeName);

Ex:
<p title="Good Morning" id = "para1"></p>

Following statements are used to get the value associated with title attribute.

var element = document.getElementById("para1");
var titleVal = element.getAttribute("title");

getAttribute.html
<!DOCTYPE html>

<html>

<head>
    <title>Get Attribute</title>
</head>

<body>
    <p title="Good Morning" id="para1"></p>

    <script>
        var element = document.getElementById("para1");
        alert(element.getAttribute("title"));
    </script>
</body>

</html>

Open above file in browser, you can able to see the value of title attribute in alert box.



Previous                                                 Next                                                 Home

No comments:

Post a Comment