Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

HTML DOM hasAttribute() method

HTML DOM hasAttribute() method

Element Object Reference Element object

Example

Check whether the button element has the onclick attribute:

document.getElementsByTagName("BUTTON")[0].hasAttribute("onclick");

Output result:

true

Try it »

Definition and Usage

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

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


Browser support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support the hasAttribute() method

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


Syntax

element

.hasAttribute(

attributename

)

Parameters

Parameters 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 Reference Element object