JavaScript element.toggleAttribute() Method

You are Here:

JavaScript element.toggleAttribute() Method

The element.toggleAttribute() method toggles the attribute on the given element.

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <input type="text" value="Change Me"> <p>Click on the button to toggle attribute between disabled and enabled</p> <button onclick="myFunction()">Toggle Button</button> <script> var elem = document.getElementsByTagName("input")[0]; function myFunction(){ elem.toggleAttribute("disabled"); } </script> </body> </html>

Syntax

element.toggleAttribute(name)

Parameter Values

ValueTypeExplanation
nameRequiredA DOMString specifying the name of the attribute to be toggled.

More Examples

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <input type="text" value="Change Me"> <p>Click on the button to toggle attribute between editable and readonly</p> <button onclick="myFunction()">Toggle Button</button> <script> var elem = document.getElementsByTagName("input")[0]; function myFunction(){ elem.toggleAttribute("readonly"); } </script> </body> </html>

Reminder

Hi Developers, we almost covered 97% of JavaScript Tutorials with examples for quick and easy learning.

We are working to cover every Single Concept in JavaScript.

Please do google search for:

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author