I'm trying to transform this little piece of code:
$(document).ready(function () {
$('.slider').slider();
});
to pure JavaScript code. I've followed some links and I've tried to rewrite the code above in two ways:
first option:
var slider2 = document.querySelectorAll(".slider");
window.onload = slider2.slider;
second option:
var slider2 = document.querySelectorAll(".slider");
document.addEventListener("DOMContentLoaded", function() {
slider2.slider();
});
In both cases I get no errors, but it doesn't show anything...
Any other ideas? :(
UPDATE: Ok, for some reason, the option with the DOM Events didn't work for me...
This is the code that I came up with that solved my problem:
var elems = document.querySelectorAll('.slider');
var instances = M.Slider.init(elems, {
inDuration : 275
});
window.addEventListener("load", instances, false);
Initially I used just window.onload = instances
, but I was using the same logic in another JS file, so I ended up overriding the other event. In the solution above, I got rid of that problem.