Arun Kumaresh Arun Kumaresh - 4 months ago 13
Javascript Question

how to get the datepicker class value in jquery?

I have a dynamic datepicker field so I have a

datepicker
class for that in which I need to get the value of
datepicker
. I have tried with
id
in which I have got the value of the
datepicker


using
id


$("#datepicker").change(function() {
selectedate= $('#datepicker').datepicker({ dateFormat: 'yy-mm-dd' }).val();
console.log(selectedate);
});


I have tried the same thing with
class
but I cannot get the value of datepicker

$(".datepicker").change(function() {
selectedate= $('.datepicker').datepicker({ dateFormat: 'yy-mm-dd' }).text();
console.log(selectedate);
});

Answer

Simples.

$(".datepicker").change(function() {
    var selectedate = $(this).val();
    console.log(selectedate);
});

this refers to the DatePicker itself within the change function parameter.

EDIT: Just seen you've had some issue between using '#datepicker' or '.datepicker'. Please post your HTML for the datepicker for help with this, as well as providing further information if the solution provided doesn't work.

N.B., for HTML-JS questions, try using JSFiddle. It'll help get answers to your questions very quickly, and maybe identify other issues in the code for you, e.g., suggesting better practices is something people do for me a lot when I post code to there, which helps me become a better programmer.

Comments