JavaScript » Document » getElementById

Syntax:
document.getElementById(id)

This element receives a string containing the id of a specific element and returns a reference to it.

After obtaining a reference to the object, you can use the getAttribute and setAttribute methods to modify the element's attributes.

Examples

Code:
<p id="myID" attr="my attribute value">

...

<script>
  var pTag1 = document.getElementById('myID');
  document.write(pTag1.getAttribute('attr') + '<br>\n');
  pTag1.setAttribute('attr', 'my new attribute value');
  document.write(pTag1.getAttribute('attr'));
</script>
Output:
my attribute value
my new attribute value
Language(s): HTML JavaScript