JavaScript - get element / node attribute value

0 points
Created by:
Dirask Community

In this article, we would like to show you how to get the element / node attribute value in JavaScript.

Quick solution:

var myElement = document.querySelector('#element-id');  // get element by id

myElement.getAttribute(attributeName);  // get the attribute value


Practical example

In this example, we will get class attribute value of the myDiv element using getAttribute() method.

// ONLINE-RUNNER:browser;

<!DOCTYPE html>
      .blue {
        background: lightblue;
    <div id="my-div" class="blue">This is my div.</div>
      var myDiv = document.querySelector('#my-div'); // get element by id
      var attributeValue = myDiv.getAttribute('class');  // get class attribute value
Native Advertising
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂