ani_css ani_css - 11 months ago 80
Javascript Question

Class instead of getElementById

I want to use the Pikaday script and it gives me JavaScript code to add to my project. Something like:

var picker = new Pikaday({ field: document.getElementById('datepicker') });

Is there any way to use a class instead of
? For example:

var picker = new Pikaday({ field: $('.datepicker') });

Answer Source

if you are only using newer browsers you can use


This will give you the first element that matches the class. Here is the Caniuse link

For older browsers you can use