ani_css ani_css - 1 year ago 134
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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download