0

I'm trying to set the value of an input element using jQuery.

HTML: <input id="id_year" name="year" type="number">

JavaScript:

$(document).ready(function () {
    var year = $("#id_year");
    year.datepicker({
        format: " yyyy", // Notice the Extra space at the beginning
        viewMode: "years", 
        minViewMode: "years"
    });
    year.on('changeDate', function(ev){
        year.attr('value',ev.date.getFullYear());
    });
});

If the input type is text, it works. But I have to use number as input type.

How can I set the input value with jQuery or regular JavaScript?

Important notes

I tried to use year.val(ev.date.getFullYear()); but It didn't work.

4
  • You shouldn't be using .attr() to set the value property of any form elements, you should be using .val(). (As an aside, why is there a space at the beginning of the format?) Commented Jun 16, 2016 at 8:25
  • try with year.val(ev.date.getFullYear()); Commented Jun 16, 2016 at 8:27
  • @KinjalGohil I tried with year.val(ev.date.getFullYear());, but It doesn't work! Commented Jun 16, 2016 at 8:29
  • Seems val can have issues sometimes? stackoverflow.com/questions/11873721/… Commented Jun 22, 2018 at 8:44

4 Answers 4

0

Your code is right, just the definition of datepicker is wrong, change

format: " yyyy"

to :

dateFormat: "yy"

(If you are using jquery-ui datepicker)

DEMO Here

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

3 Comments

Your works, my script not. Perhaps because I am using bootstrap-datepicker.js bootstrap-datepicker.readthedocs.io/en/latest/index.html
Using ev.date.getFullYear() I get a valid year number. I do not understand why I can not change the input value. If the type is "text" it works, but not if the type is "number".
I chose to use the jquery-ui library. Thanks
0
year.val(ev.date.getFullYear());

to get the value: var yearValue = year.val();

http://api.jquery.com/val/

Comments

0

You have to use the val function.

You can write your code as this:

$(document).ready(function () {
    var year = $("#id_year");
    year.datepicker({
        format: " yyyy", // Notice the Extra space at the beginning
        viewMode: "years", 
        minViewMode: "years"
    });
    year.on('changeDate', function(ev){
        console.log("ssds");
        $(this).val(ev.date.getFullYear());
    });
});

Comments

0

You can use like this.

$(document).ready(function () {
    var year = $("#id_year");
    year.datepicker({
        format: " yyyy", // Notice the Extra space at the beginning
        viewMode: "years", 
        minViewMode: "years",
        onSelect: function(dateText) {
          console.log(dateText);
          var startDate = new Date(dateText);
          year.val(startDate.getFullYear());
          console.log(year.val());
        }
    });
   /* year.on('changeDate', function(ev){
        console.log("ssds");
        year.val(ev.date.getFullYear());
      console.log(year.val());
    });*/
   
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>

<link rel="stylesheet" href="https://jqueryui.com/resources/demos/style.css">
<input id="id_year" name="year" type="number">

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.