2

I am trying to loop through all input=text fields that have a specific class and extract the value of the input fields using jQuery. I can't seam to get the value though as no matter what, it gives me "(an empty string)" in the console.

How can I get the value? My code is:

$.each($('.datepicker'), function() {
    console.log(this);
    console.log($(this).val());
});

The output of this is:

<input id="createdDate" class="datepicker hasDatepicker" type="text" onchange="setDueDate();" value="2013-06-19" size="8" name="invoice[createdDate]">
(an empty string)
<input id="dueDate" class="datepicker hasDatepicker" type="text" value="2013-06-19" size="8" name="invoice[dueDate]">
(an empty string)

Edit: It seams the issue was part cached JS (even though the file was showing correctly in the source, it was executing the older JS...very odd), and part the jQuery datepicker screwing things up. I fixed it like so:

$(document).ready(function() {
    $('.datepicker').datepicker();

    $.each($('.datepicker'), function() {
        date = $(this).val();

        $(this).datepicker('option', 'dateFormat', 'yy-mm-dd');
        $(this).datepicker('setDate', date);
    });
});
1
  • just set up a fiddle with your code and it is working as expected - jsfiddle.net/YR892 Commented Jun 20, 2013 at 8:52

3 Answers 3

6
$('.datepicker').each(function(index, item) {
    console.log($(item).val());
});

should work

Sign up to request clarification or add additional context in comments.

Comments

1
$('.datepicker').each(function(){
   console.log(this.value);
});

Comments

0
$(.datepicker).each(function(index, element)){
    console.log($(element).val());
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.