0

HTML

<div class="slider-container slide-0" num-slides="5" rel="0"></div>

JS

var maxNum = parseInt($('.slide-0').attr("num-slides"));
// maxNum = NaN

var maxNum = parseInt($('.slide-0').attr("num-slides").value);
// "Uncaught TypeError: Cannot read property 'value' of undefined"

var maxNum = parseInt($('.slide-0').attr("num-slides").val());
// Uncaught TypeError: Cannot read property 'val' of undefined

var maxNum = Number($('.slide-0').attr("num-slides"));
// maxNum = NaN

Currently:

When I run the program in Google Chrome 64.0, parseInt and Number does not return a numeric value. However, when I type the code into the dev tools console:

parseInt($('.slide-0').attr("num-slides"));
// returns 5 (not string)

Any ideas why this might be the case?

Etep
  • 2,721
  • 4
  • 17
  • 28
  • $('.slide-0') does return more than an element .. I think you need to loop through a collection or .. try to use a html Id – M. Gara Feb 07 '18 at 20:47
  • 3
    From the errors on the second two attempts, it looks like there is no such element at the time your code executes. My guess is that you are running this before the html has finished loading. If this code is part of a script tag, where is that tag located in your html? – CRice Feb 07 '18 at 20:48
  • 1
    @CRice you're right. It was because the script was running before the HTML was finished loading. Thanks! – Etep Feb 07 '18 at 21:12

0 Answers0