class method Element.readAttribute

View source on GitHub →

Element.readAttribute(element, attributeName) → String | null

Returns the value of element's attribute or null if attribute has not been specified.

This method serves two purposes. First it acts as a simple wrapper around getAttribute which isn't a "real" function in Safari and Internet Explorer (it doesn't have .apply or .call for instance). Secondly, it cleans up the horrible mess Internet Explorer makes when handling attributes.

Examples
<a id="tag" href="/tags/prototype" rel="tag" title="view related bookmarks." my_widget="some info.">Prototype</a>

Then:

$('tag').readAttribute('href');
// -> '/tags/prototype'
 $('tag').readAttribute('title');
// -> 'view related bookmarks.'
 $('tag').readAttribute('my_widget');
// -> 'some info.'

This method can be called either as an instance method or as a generic method. If calling as a generic, pass the instance in as the first argument.