to set a "hash" attribute for HTML anchor element "a"
The <a> element (HTMLLinkElement) already has a DOM Level 0 hash
property. It is used like window.location.hash to read or set the ‘...#anchor’ part at the end of the URL being referred to by the element's href
.
Setting a.hash
, whether directly or through jQuery's attr()
wrapper, merely sets the anchor name in the link's URL. You could deliberately say you want an actual attribute by calling the DOM method a.setAttribute('hash', value)
, except that this doesn't work in IE6/7 due to a long-standing bug where it confuses attributes and properties.
This is one of the problems with adding custom non-standard attributes to elements, you never know when it's going to clash with an existing name. HTML5 will suggest you limit your custom attributes to names starting with ‘data-’, but in general it's best to find another way of storing data if you can.