Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use element.hasAttribute("id") to test whether a DOM element contains an id attribute. It returns true for both a normal value such as id="main" and a present-but-empty attribute such as id="".

Check whether the id attribute exists

if (element.hasAttribute("id")) {
  console.log("This element has an id attribute");
}

hasAttribute() returns a Boolean. It checks attribute presence, not whether the value is useful or nonempty.

Read the value and detect a missing attribute

const id = element.getAttribute("id");

if (id !== null) {
  console.log("The attribute exists. Its value is:", id);
}

getAttribute("id") returns the attribute value as a string. When the attribute is absent, it returns null; when markup contains id="", it returns an empty string. See MDN’s getAttribute() documentation.

Choose the right DOM operation

Need Use Result and caveat
Test whether this element has an id attribute element.hasAttribute("id") Boolean; distinguishes missing from present-empty.
Read the value and detect absence element.getAttribute("id") String or null when absent.
Read the reflected ID value concisely element.id Reads the element’s reflected id content attribute. See MDN’s Element.id documentation.
Find an element when its ID is already known document.getElementById("name") Searches the document; it is a lookup, not a presence test on an existing element.

Test for a nonempty id

If your requirement is specifically “the element has an ID value,” reject both a missing attribute and an empty value:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#1 Best Overall
Sale
HTML and CSS: Design and Build Websites
  • HTML CSS Design and Build Web Sites
  • Comes with secure packaging
  • It can be a gift option
const hasNonemptyId =
  element.hasAttribute("id") && element.getAttribute("id") !== "";

For a concise value check, you can also use element.id !== "". Use the two-step form when distinguishing attribute presence from its value is important.

Select the element before checking it

The check operates on an element object. Select or traverse to that element first:

const element = document.querySelector("section");

if (element && element.hasAttribute("id")) {
  console.log(element.getAttribute("id"));
}

The element && guard prevents an error when the selector found nothing. If the ID is already known and you want the matching element, use:

const element = document.getElementById("main");

if (element) {
  console.log("Found:", element);
}

HTML IDs are intended to identify elements for links, scripts, and styles. A nonempty ID should be unique in its document tree. The HTML Standard states that an ID must be unique among IDs in the element’s tree and contain at least one character when specified: WHATWG HTML Standard. If duplicate IDs exist, getElementById() returns the first matching element; see MDN’s getElementById() documentation.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Use IDs safely in CSS selectors

An HTML ID value can be valid as an attribute while still requiring escaping in CSS selector syntax. Escape dynamic values before passing them to querySelector() or placing them in a selector:

const id = element.getAttribute("id");

if (id !== null) {
  const match = document.querySelector(`#${CSS.escape(id)}`);
}

For example, an ID containing characters that have special meaning in CSS may fail in an unescaped selector even though the HTML attribute exists. The MDN id global-attribute reference covers the relationship between HTML IDs and selector syntax.

Rank #4
Sale
Web Design with HTML, CSS, JavaScript and jQuery Set
  • Brand: Wiley
  • Set of 2 Volumes
  • A handy two-book set that uniquely combines related technologies Highly visual format and accessible language makes these books highly effective learning tools Perfect for beginning web designers and front-end developers

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.