Working with attributes via methods in JavaScript

You already know how to get tag attributes as properties of DOM elements. There is also an alternative option - using special methods. Let's explore these methods.

Getting

Given an element:

<input id="elem" value="text">

Get the value of its value attribute.

Given an element:

<input id="elem" class="www zzz">

Get the value of its class attribute.

Setting

Given an element:

<input id="elem">

Set its value attribute to 'text'.

Given an element:

<input id="elem">

Set its class attribute to 'valid'.

Removing

Given an element:

<input id="elem" value="text">

Remove the value attribute from it.

Checking

Given an element:

<input id="elem" value="text">

Check if it has the value attribute.

enru