2

I have multiple text inputs like this:

<input type="text" class="datepicker" value="11-11-2016">

And the jquery script:

  $(".datepicker").each(function() {
    $(this).datepicker();
    $(this).datepicker("option", "changeYear", true);
  });

The input value disappears immediately after the page loads!!!

If I remove the option:

$(this).datepicker("option", "changeYear", true);

... the value doesn't dissapear, but whenever I add any of the jquery datepicker options - the value dissapears!?

1
  • @bresleveloper Please stop bothering people for plunkers or jsfiddles. The code has to go in the question, not on an external code hosting service. Commented Jul 28, 2016 at 14:33

5 Answers 5

1

I think the problem it's in your initialization, try this:

$(function(){

    $('.datepicker').each(function(){
        $(this).datepicker({"changeYear": true});
    });

});

Here's a working fiddle

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

1 Comment

According to the official documentation, you can add the option after the initialization. I want to use that method for other purposes.
1

Try this line afterwards to rectify the issue:

$(".datepicker").each(function() {
    $(this).datepicker();
    $(this).datepicker("option", "changeYear", true);
    $(this).attr("value", "11-11-2016");
});

If the attributes are dynamic per $(".datepicker"), then use the following:

function getElemAttributes(var element) {
    var attrs = {};
    var attrMap = element.attributes;

    $.each(attrMap, function (i, e) { attrs[e.nodeName] = e.nodeValue; });

    return attrs;
}

to get all of the attributes as an object. So:

$(".datepicker").each(function() {
    $(this).datepicker();

    // Returns something like { id: "datepicker", ..., value: "11-11-2016" }
    var originalAttributes = getElemAttributes(this);

    // Do stuff that affects attr on element.
    $(this).datepicker("option", "changeYear", true);

    // Set element attributes from riginal attributes object.
    if (originalAttributes.hasOwnOroperty("value")) {
        $(this).attr("value", originalAttributes["value"]);
    }
    else {
        // Didn't originally have a "value" attribute - set some default here?
    }
});

1 Comment

First I get Uncaught SyntaxError: Unexpected token var, after I remove var I get Uncaught TypeError: originalAttributes.hasOwnOroperty is not a function
1

You should use this option to set the initial date:

$( ".selector" ).datepicker( "setDate", "10/12/2012" );

in you code example:

  $(".datepicker").each(function() {
    $(this).datepicker();
    $(this).datepicker("option", "changeYear", true);
    $(this).datepicker( "setDate", "10/12/2012" );
  });

1 Comment

When I use this with $(this).val() instead of the date you made up, I get wrong date because of the format (I used "11-11-2016" in my value). If I first set the format option, the value disappears ... again.
0

You need this:

$(document).ready(function () {

$('.datepicker').each(function(){
    $(this).datepicker();
    $(this).datepicker("option", "changeYear", true);
    $(this).datepicker( "setDate", $(this)[0].getAttribute('value') );
});

Comments

0

This command helped me with the problem like yours:

let date = $(this.dateInput.nativeElement).val().toString();
....
$(this.dateInput.nativeElement).datepicker().val(date).trigger('change');

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.