JavaScript Tutorial
JS - Introduction JS - Basic JS - Placement JS - Output JS - Statements JS - Syntax JS - Variables JS - Comments JS - Data Types JS - Operators JS - Operator Precedence JS - Condition JS - Switch JS - Functions JS - Objects JS - Loops For JS - Loops While JS - Break JS - Strings JS - String Methods JS - Numbers JS - Number Methods JS - Arrays JS - Array Methods JS - Array Iteration JS - Events JS - Event Listener JS - Event Propagation JS - Date JS - Math JS - Random JS - Boolean JS - Type Conversion JS - RegExp JS - Exception JS - Scope JS - Debugging JS - Hoisting JS - Strict Mode JS - this Keyword JS - Mistakes JS - Best Practices JS - Performance JS - Form Validation JS - ES6 Features
JS Objects
Object Definitions Object Properties Object Methods Object Constructors Object Prototypes
JS Functions
Function Definitions Function Parameters Function Call Function Apply Function Closures
JS HTML DOM
DOM Introduction DOM Methods DOM Selectors DOM HTML DOM CSS DOM Attributes DOM Navigation
JS Browser BOM
JS - Window JS - Screen JS - Location JS - History JS - Navigator JS - Popup Alert JS - Timing JS - Cookies
JS AJAX
AJAX - Introduction AJAX - XMLHttp AJAX - Request AJAX - Response AJAX - PHP
JS JSON
JSON - Introduction JSON - Syntax JSON - Data Types JSON - Parse JSON - Stringify JSON - Objects JSON - Arrays JSON - PHP JSON - JSONP
JS References
JavaScript Reference JavaScript Methods HTML DOM Reference

JavaScript HTML DOM - Changing Attributes

The HTML DOM allows JavaScript to get and change the attributes of HTML elements.


Getting Element's Attribute Value

The getAttribute() method is used to get the current value of a specified attribute on the element.

The following example gets the value of the href and title attributes of an anchor element:

Run code

Setting Attributes on Elements

The setAttribute() method is used to set the value of an attribute on the specified element.

If the attribute already exists, the value is updated; otherwise a new attribute is added with the specified name and value.

This example sets the value of the href attribute to an anchor element:

Run code

Similarly, you can use the setAttribute() method to update or change the value of an existing attribute on an HTML element.

Run code

Removing Attributes from Elements

The removeAttribute() method is used to remove an attribute from the specified element.

This example removes the href attribute from an anchor element:

Run code