DOM - Node attribute

> (World Wide) Web - (W3|WWW) > The Document Object Model W3C API Interface (DOM)

1 - About

attributes representation in the DOM.

Node attributes are not included as children in the DOM hierarchy but are properties of an element.

Advertising

3 - Management

3.1 - Get

  • The HTML with the style attribute that we want to change
<p>My sweat suit is <span style="color: green">green</span> </p>
spanElement = document.querySelector('body>p>span');
  • Get the attribute value
style = spanElement.getAttribute ('style');
console.log("The style attribute has the value: ("+style + ")");
  • Result:

3.2 - Set

  • The HTML with the style attribute that we want to change
<p>My sweat suit is <span style="color: green">green</span> </p>
spanElement = document.querySelector('body>p>span');
  • Set the attribute to the color red
spanElement.setAttribute ('style','color:red');
  • Result:

Advertising

3.3 - Remove

web/dom/attribute.txt · Last modified: 2019/02/09 12:15 by 162.158.78.98