Leon van der Veen
Leon van der Veen

Reputation: 1672

check for changes in form via Ajax

I've got a little auto-suggest jQuery script. What it does is when I start typing it looks into my database for matches via an Ajax Post. When I click on a result the input field get the value wich I picked.

That works but what I want is to check if the value of the input has changed its value.

What i tried is

$("input").change(function(){
   alert('change'); 
}); 

but it doesnt work.

Is there a way to check if a form input field has changed his value from an AJAX POST?

Upvotes: 2

Views: 1024

Answers (2)

user2334807
user2334807

Reputation:

.changed is not a function in jQuery.

Here the function is .change use that:

$("input").change(function(){ 
    alert('change'); 
});

Use the following function instead of above:

$(document).ready(function(){
$(".ui-autocomplete").click(function(){
     alert("changed the input field.");
});

});

I am using the class ui-autocomplete as you told that you are using the jQuery autocomplete. You can detect the name of class or id by using the firebug.

Upvotes: 3

Mark Essel
Mark Essel

Reputation: 4646

Triggering a Change Event

Only a few event types automatically activate a change event. For your particular application you may need to trigger the event manually as described below.

from the jquery documentation

The change event is sent to an element when its value changes. This event is limited to elements, boxes and elements.

To trigger the event manually, apply .change() without arguments:

$('#other').click(function() {
  $('.target').change();
});

I wrote a small utility for myself which monitors object states and propagates state changes: Oculus. It is currently used only in a small client side project, but it's a simple enough pattern to be reusable.

Upvotes: 0

Related Questions