0
votes

I am trying to access the data attributes in js. But displays the error="Uncaught type error"

var x=document.getElementById(this.id);
console.log(x.src);
console.log(x.data('image'));

for x.src I m getting perfect-"http://localhost:3000/uploads/asset/asset3/199/small_ring3.jpg", but when I try for x.data('image'), it displays - "Uncaught TypeError: undefined is not a function58:646 (anonymous function)jquery.min.js:3 m.event.dispatchjquery.min.js:3 m.event.add.r.handle"

here x is

img alt="Small ring3" class="img-thumbnail" data-image="/uploads/asset/asset3/199/large_ring3.jpg" id="2" src="http://localhost:3000/uploads/asset/asset1/199/large_ring1.jpg"

Please help.

1
A bracket ')' is missing at the end of the second console.log() call - Phate01
x is not a jquery object as far as I can see, try var x = $('#'+this.id); - GrĂ¼se
console.log(x.data('image')); Sorry thats typo error here, not in my code. Its displaying the "Uncaught TypeError: undefined is not a function" - dips

1 Answers

2
votes

use getAttribute()

console.log( x.getAttribute('data-image') );