A <img /> element has a data-id attribute that I want to retrieve using jQuery. I believe the .data() function can do this.
$('.photo').get(0).data('id')
Problem: When I tried using .data('id') to retrieve the attribute, I get the error:
Uncaught TypeError: Object #<HTMLImageElement> has no method 'data'
Where did I go wrong?
jsfiddle: http://jsfiddle.net/KLG3R/
like others said, your problem is the
.get(0)which returns the HTML element itself, rather than a jquery object. To get a certain element, you use the:eqpseudo selector like:http://jsfiddle.net/gunderson/KLG3R/2/