.attr('data-caption') is the getter for the given attribute; to set it, you’ll also have to pass a value, like .attr('data-caption', 'my description')
As @Dormilich noted, you can also simply use the .data() method; however, the difference is that it only stores the dataset internally, so you can’t use it as a selector (say). Consider:
So which to use depends on what you’d like to do with it. .data() is more versatile for storing data (you can access the full dataset as a single object), but it doesn’t actually get written to the DOM.