I’m trying to get a data-attribute of an image on my page using the following code, but it doesn’t work
var rand_el = $('img').get(Math.floor(Math.random() * $('img').length)).attr('data-id');
But I just get the following console log:
Uncaught TypeError: Object
#<HTMLImageElement>has no method ‘attr’
Any ideas?
getreturns a DOM element reference. Useeqto reduce the set of matched elements to the one at the specified index while still returning a jQuery object.