This page is Almost Ready

Notice: The WebPlatform project, supported by various stewards between 2012 and 2015, has been discontinued. This site is now available on github.

hasAttribute

Summary

Determines whether a content attribute exists on an element.

Method of dom/Elementdom/Element

Syntax

var attributeExists = element.hasAttribute(name);

Parameters

name

Data-type
String

The name of the attribute.

Return Value

Returns an object of type BooleanBoolean

Whether the specified attribute exists.

Examples

// Get the first div element in the page.
var element = document.querySelector("div");
// Continue only if the element exists.
if (element) {
 console.log("Does the first div have a data-example attribute? " + element.hasAttribute("data-example"));
}

Usage

 Use this method to determine whether a content attribute exists on an element.

Notes

  • This method does not get the value of the attribute, see getAttribute for this purpose.
  • See hasAttributes, which determines whether the element has any attributes at all.

Related specifications

Document Object Model (DOM) Level 3 Core
Recommendation
Document Object Model (DOM) Level 2 Core
Recommendation
DOM
Living Standard

Attributions