Easy Tutorial
❮ Prop Element Tagname Event Onclick ❯

HTML DOM hasAttribute() Method

Element Object

Example

Check if a button element has the onclick attribute:

Output result:

Definition and Usage

The hasAttribute() method is used to determine if a specified attribute exists. It returns true if the attribute exists, otherwise it returns false.

Note: We can use setAttribute() to add a new attribute or modify an existing attribute in the element.


Browser Support

All major browsers support the hasAttribute() method.

Internet Explorer 8 and earlier versions do not support this method.


Syntax

Parameters

Parameter Type Description
attributename String Required. The name of the attribute you want to check.

Return Value

Type Description
Boolean Returns true if the element has the attribute, otherwise returns false.

Technical Details

| DOM Version | Core Level 2 Element Object | | --- | --- |


Element Object

❮ Prop Element Tagname Event Onclick ❯