Latest web development tutorials

HTML DOM hasAttribute () method

Elements Object Reference Element object

Examples

Check whether the button elements have onclick attribute:

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

Output:

true

try it"

Definition and Usage

getAttribute () method to get the value of an attribute by name.


Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support hasAttribute () method

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


grammar

element .hasAttribute( attributename )

parameter

参数 类型 描述
attributename String 必须。你要检查的属性名。

return value

类型 描述
Boolean 如果元素有属性返回 true, 否则返回 false。

technical details

DOM version Core Level 2 Element Object


Elements Object Reference Element object