How to use DOM Element SetAttribute in JavaScript

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

JavaScript DOM Element setAttribute() Method

JavaScript setAttribute() method use for set specified attribute instead of old attribute and give specified value. If both attribute are same then only value will be change.

Syntax

element.setAttribute(attribute name, attribute value)

Example

<html>

<head>

    <script type="text/javascript">

 

        function myfun() {

            document.getElementsByTagName("INPUT")[0].setAttribute("type", "radio");

        };

 

    </script>

</head>

<body>

    <input value="hello" />

    <p id="demo">

        click button to set attribute</p>

    <button onclick="myfun()">

        click me</button>

</body>

</html>

 

Output

 

Before click

 

 setAttr1.jpg

 

After click

 

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