-1

I have a datepicker that is setting the value of a textbox. Every time the text in that text box changes, I want to submit the form. Here is a snippet of the code that I thought would achieve this:

$(document).ready(function() {
  watchDateFields();
}

function watchDateFields() {
  $('form .date_field').on('change', function() {
    this.form.submit();
  }
}

The .date_field input gets dynamically set, but for some reason this doesn't fire the change event?

Bebbs
  • 289
  • 2
  • 13
  • Setting the value of an input with javascript (dynamically) does not fire the `change` event. You'll have to hook into whatever is setting the value, or try and post that code here. – adeneo Oct 20 '15 at 11:37
  • 3
    @kosmos You should read the question better. They aren't creating the element dynamically, they're setting the value. – JB06 Oct 20 '15 at 11:38
  • Follow http://stackoverflow.com/questions/16667105/onchange-event-not-working-when-change-automated – murli2308 Oct 20 '15 at 11:38
  • I see, my fault. Too many questions of that kind. Deleting duplicate comment.. – kosmos Oct 20 '15 at 11:40
  • Which datepicker are you using? – Salman A Oct 20 '15 at 11:46
  • 1
    Based on the answers, there must be some confusion here? Is the element added dynamically, or is it the value that is set dynamically, or more correct, programatically. – adeneo Oct 20 '15 at 11:49

4 Answers4

4

You need to attach it to a static element:

$(document).on('change', 'form .date_field', function() {
  this.form.submit();
}

Or, better replace document with something that is static. Eg:

$('form').on('change', '.date_field', function() {
  this.form.submit();
}

Or you might need to attach the event with the plugin's event handlers, say:

$('form .date_field').on('datechange', function() {
  this.form.submit();
}
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
1

The change event will only trigger once, ie, when a user enters a new value into the input and then focuses away.

If you want this to update as a user types, use the keydown event.

However, submitting a form every time a key is pressed is not a good idea. What is the result you are trying to achieve? Consider at the very least validating the input or checking certain conditions are met before you submit the data (eg, make sure the input is at least 10 characters long).

duncanhall
  • 11,035
  • 5
  • 54
  • 86
0

Place your textfiels inside a div element and attach the change event to this div:

$('#mydiv').on('change', '.date_field',  function(){
       this.form.submit();
});

Now the mydiv element will be watching for the change envents for all his child elements, no matter if they are dynamically generated.

anmarti
  • 5,045
  • 10
  • 55
  • 96
0
$( "input" ).keyup(function() {
  //It's changed
});

Easy way to track any change in anywhere... Many framework with 2 ways data binding, use keyup in Javascript to $watch the changes...

Also jQuery UI date-picker has onSelect...So easy way to do ur job... look at https://jqueryui.com/datepicker for more information...

Alireza
  • 100,211
  • 27
  • 269
  • 172