How to use DOM Element SetAttributeNode in JavaScript

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

JavaScript DOM Element setAttributeNode() Method

JavaScript setAttributeNode() method use for set specified attribute node to an element. If attribute already exists on element then then its will replaced it.

Syntax

element.setAttributeNode(attributeNode)

Example

<html>

<head>

    <style type="text/css">

        .sty

        {

            color: blue;

            font-family: Arial;

            text-decoration: underline;

        }

    </style>

    <script type="text/javascript">

 

        function myfun() {

            var mm = document.createAttribute("class");

            mm.nodeValue = "sty";

            var pp = document.getElementsByTagName("H1")[0];

            pp.setAttributeNode(mm);

        };

 

    </script>

</head>

<body>

    <h1>

        Hello India</h1>

    <p>

        click button to set attribute to node</p>

    <button onclick="myfun()">

        chenge it</button>

</body>

</html> 

 

Output

 

Before click

 

 SetAttNode1.jpg

 

After click

 

 setAttrNode2.jpg

 

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.