0

I'm trying to get the data attribute value thats on an image and then use that value to set my slider speed, how do I get this value? I tried the below but console.log returns undefined..

var sliderSpeed = $('logo').data('data-speed');

console.log(sliderSpeed);

$('.infinite-slider').infiniteslide({
    'speed': sliderSpeed,
    clone: 10
  });
<li><img class="logo full-width" src="" data-speed="100"></li>

birdyoung
  • 197
  • 2
  • 17

4 Answers4

1

You forgot the '.' for $('.logo')

var sliderSpeed = $('.logo').data('speed');

console.log(sliderSpeed);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<li><img class="logo full-width" src="" data-speed="100"></li>
Sven.hig
  • 4,449
  • 2
  • 8
  • 18
0

User the attr method:

var sliderSpeed = $('.logo').attr('data-speed');
Andrew Arthur
  • 1,563
  • 2
  • 11
  • 17
0
var sliderSpeed = $('logo').data('speed');

The data function by default fetches all data attribute. To filter a specific data attribute, pass attribute name as parameter to data function.

0

You dont use correct selector, if you selecting $('logo') jquery is looking for element with tag logo not with class logo.

if you want to select el by class name it should be $('.logo')

to get data-whatever attribute simply use $('.logo').data('speed'), you can also getting value using attr $('.logo').attr('data-speed')