-3

HTML

<figure class="video-thumb" 
  data-thumb-0="image2.jpg" 
  data-thumb-1="image3.jpg"
  data-thumb-2="image4.jpg"
  data-thumb-3="image5.jpg"
> 
  <img src="image1.jpg"> 
</figure>

I want to get each of these data-thumb-* attributes with jQuery.

I then want to iterate through these using setInterval to swap the src of image1.jpg.

I will end up with the user hovering and seeing the data-thumb-0/1/2/3/4/etc, over the duration of a few seconds.

The reason I have these numbered (data-thumb-0/1/2/3/4/5) is because the CMS pushes these numbers out based on how many thumbs the client has uploaded.

I hope this is enough information, any help seriously appreciated.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
sansome
  • 19
  • 1
  • 5

1 Answers1

1

Assuming you have your element in a variable such as elem, try this:

var thumbs = [], i = 0, attr;
while( attr = elem.getAttribute("data-thumb-"+i)) {
    thumbs[i] = attr;
    i++;
}

This will give you an array of thumbnails.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • Did not want to use javascript as am not as comfortable. Adjusted slightly to work - http://jsfiddle.net/tomsansome/hGxse/. Thanks very much Niet. – sansome Jan 08 '14 at 11:16
  • 1
    If you're not comfortable with basic JavaScript, you should not be using jQuery... – Niet the Dark Absol Jan 08 '14 at 11:47
  • You are getting good advice here, I would take it if I were you. You REALLY shouldn't be using jquery if you don't even understand the basics of javascript. Going your intended route will only result in headaches later. I myself went down the same path once, and it was indeed a mistake. Jquery is a tool to utilize once you have a proper grasp of the concepts. – Ray Nicholus Jan 08 '14 at 13:33
  • maybe I should have written **Did not want to use _pure_ javascript as am not as comfortable**... – sansome Jan 08 '14 at 13:46
  • Again, you really should be using pure javascript. Once you have a good understanding, then maybe pickup a tool like jquery. – Ray Nicholus Jan 08 '14 at 13:53