I am writing a jQuery function for an image gallery with thumbnails. There is a div at the top of the webpage that will display a larger version of a clicked thumbnail as well as a title and a description.
When the thumbnails are clicked, the title changes as I expect but the description remains the default (defined by HTML).
The thumbnails are in an unordered list like so:
<ul class=thumblist id=thumblist>
<li><a title="thumb1" description="this is the first pic"></a></li>
<li><a title="thumb2" description="this is the second pic"></a></li>
</ul>
Here is the jQuery function:
$('.thumblist a').click(function() {
$("#title").text(this.title);
$("#description").text(this.description);
});
What can I do to fix my markup and achieve the functionality I am looking for?
You want the
attrfunction:The reason it sort of half-worked is that
titleis a valid HTML attribute that has a reflected property on the DOM element instance, sothis.titlegave you that attribute; butdescriptionis not, so there’s no reflecteddescriptionproperty. Usingattrgoes and looks for the actual attribute value.Ideally, use
data-*attributes for ad-hoc attributes (like yourdescription) not defined by any standard.