views:

31

answers:

3

I have an INPUT element of the TEXT type and I want to perform an action whenever the INPUT's value does not equal a default value. How do I do this? I've looking into jQuery's "change" event handler, but it only works for SELECT elements. Anyhow, here is what I want to do..

Example:

<input type="text" name="test" value=\"Default..\" />

$('input[name=test]').FireWhenValueIsChangedThroughTyping() {
 if (value!='Default..') { DoStuffHere(); }
}
A: 

You can use the blur event for that. It gets triggered when the user moves away from the input. If you need triggers as the user types, then use the keypress event. (See the Examples tab on the linked pages.)

Zed
+2  A: 

You can use the keydown function:

$('input[name=test]').keydown(function() {
 if (value!='Default..') { DoStuffHere(); }
});

You probably want to bind to change as well (which does work for text inputs - it fires on blur) - someone could paste using the mouse, for example.

Greg
A: 

A solution as $.fn:

$.fn.FireWhenValueIsChangedThroughTyping = function(defaultvalue){
 $(this).keydown(function(){
  if(this.value != defaultvalue){
    //DoStuffHere
  }
 });
};
eskimoblood