Skip to main content Accessibility Feedback

Attributes & Properties

Element.hasAttribute()

Check for the existence of an attribute (including data attributes) on an element.

let elem = document.querySelector('#lunch');

if (elem.hasAttribute('data-drink')) {
	console.log('Add a drink!');
}

Preorder my new course on Web Components! Want to learn how to build Web Components from scratch, master best practices, and more? Preorder today and get $100 off of the launch price.


Find this useful? You can support my work by purchasing an annual membership.