jquerythisjquery-data

jquery data attribute get this element


I have a view with a lot of images

 <img src="@Path" data-img-prev="@UrlPreview" data-full-path="@UrlImage" onclick=Get()  />

and it render in html like this

<img img-prev="1.jpg" data-full-path="https://...1.jpg" src="https://...1.jpg">
<img img-prev="2.jpg" data-full-path="https://...2.jpg" src="https://...1.jpg"> 
<img img-prev="3.jpg" data-full-path="https://...3.jpg" src="https://...1.jpg"> 

I want function to get data from each element

function Get() {
this.getAttribute('img-path');
this.data('img-path');

}

It is not working please help:

error this.getAttribute is not a function


Solution

  • The data attribute is called data-full-path, not img-path.

    var result = this.getAttribute('data-full-path');
    

    or since you tagged it with jQuery (assumings you're in a click handler context):

    var result = $(this).data('full-path');