1

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

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user246340
  • 45
  • 1
  • 7

2 Answers2

2

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');
Johan
  • 35,120
  • 54
  • 178
  • 293
  • thank you $(this).data('full-path'); it is works and this.getAttribute('data-full-path'); error getAttribute is not a function – user246340 Jan 08 '14 at 12:01
  • @user246340 You need to check if it's supported in your browser if you don't want to user jQuery: `if('getAttribute' in this)` should be enough. – Johan Jan 08 '14 at 15:02
  • it is work in ie 9-10 firefox and chrome last version I work only in this browsers – user246340 Jan 09 '14 at 14:06
1

Use .data('full-path') instead of .data('img-path')

Anthony Graglia
  • 5,355
  • 5
  • 46
  • 75