JavaScript hasAttribute() Method Tutorial

In this section, we will learn what the Element hasAttribute() method is and how to use it in JavaScript.

What is Element hasAttribute() Method in JavaScript?

The JavaScript Element hasAttribute() method is used to check and see if an element has set a specific attribute.

Note: using this method, we can only check the existence of an attribute! But if you want to set a new attribute for an element, you can use the `setAttribute()` method instead. Also, if you want to get the value of an attribute, you can use the getAttribute() method.

Element hasAttribute() Method Syntax:

element.hasAttribute(attribute-name);

Element hasAttribute() Method Parameter

The method takes one argument and that is the name of the attribute we want to check its existence in the target element.

Element hasAttribute() Method Return Value

The return value of this method is of type boolean.

The value true means the target element has this attribute already.

The value false means otherwise.

Example: using hasAttribute() method in JavaScript

See the Pen using hastAttribute() method in JavaScript by Omid Dehghan (@odchan1) on CodePen.

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies