I made a choice of days from the calendar using the datepicker. Marking days on the calendar - the date is recorded in the input through zyapatyuyu.

<input type="text" id="altField" value="2015-09-25, 2015-09-26"> 

Everything is good. It works, but I wanted to see the number of selected days displayed below. With the help of lenght decided to count the number of words, Tipo days.

 <div id="result"></div> <script> function dayCount( val ){ return { days: val.match(/\S+/g).length } } var $div = $('#result'); $('#altField').on('input', function(){ var c = dayCount( this.value ); $div.html( "<br>Дней: "+ c.days ); }); </script> 

But does not work. Suppose if I enter words (dates) from the keyboard or copy them into the input field, I think it is fine, but if the value gets there from the calendar it doesn’t notice it. What to do? :(

  • Subscribe to change , then you will catch changes. Here I described a little how events work - BOPOH
  • I tried to add change for everyone, but then I don’t react at all. Neither from Klava nor from the calendar - akasergej
  • I entered all possible values ​​that google bind ("propertychange change click keyup input paste", function () reacts to the keyboard, and to mouse click in the input field after there the values ​​from the calendar appear ... just after selecting the date it still does not respond :( - akasergej
  • 3
    When you change the value with the knobs, change is called when the element loses focus. But you change an element through js? It's a little more difficult with him - change only works when you change an element in the browser. When you change it through js, you have to throw the event yourself. For example: var elem = $('#elem-id'); elem.val('123').change(); var elem = $('#elem-id'); elem.val('123').change(); - BOPOH
  • Hooray! Thanks for explaining, it worked !! :) - akasergej

3 answers 3

When you change the value with the knobs, change is called when the element loses focus. But you change an element through js? It's a little more difficult with him - change only works when you change an element in the browser. When you change it through js, you have to throw the event yourself. For example:

 var elem = $('#elem-id'); elem.val('123').change(); 

    You can even a space to indicate all the events to which you want to respond. Like this:

     $('#altField').on('input change keyup', function () {}); 

    In addition, there is such a useful jQuery library text change custom event . In addition to the main events, like change, it is able to respond to insert events from the buffer, etc.

      You can Old-school 'but use var x = myVar.split(delimiter); console.log(x.length) var x = myVar.split(delimiter); console.log(x.length) for word counting