tags:

views:

15

answers:

2

I've got a dirty text variable I need to set on a text change, radio button change or dropdownlist change in a form. Unfortunately the generic:

$(':input').live('change',function(){... }) 

also detects a change in focus... thus making a false positive for dirty text.

A: 

The callback function takes an eventType as the first argument. Check to see if it's focus and ignore it if so. See the method declaration.

Jason McCreary
A: 

You've got bigger problems, in that change will not detect text change unless there is a change in focus. Radio buttuns and drop down lists will trigger change instantly but not text.

From the jQuery .change() docs:

The change event is sent to an element when its value changes. This event is limited to elements, boxes and elements. For select boxes, checkboxes, and radio buttons, the event is fired immediately when the user makes a selection with the mouse, but for the other element types the event is deferred until the element loses focus.

For text the usual solution is to check for a change on each .keyup()

To make sure you are not doing it on blur you can use event.type not being blur

$(selector).change(function(event) {
    if (event.type == "blur") return false;
    ....
});

but this would only be needed in a .change() not if you are checking for keypresses like below:

So for text, you'd have to do something like:

$(function() {                         // <== Doc ready  

    var inputVal = $("input").val();   // a variable to hold the text
                                       // set it up w the initial value then see 
                                       // if the input value changes.

    $("input").keyup(function(event) {

        // check for change of the text field after each key up
        if(this.value != inputVal)
        {
            // Do your search, etc.
            // ...

            // Reset the temp value for the next comparison
            inputVal = this.value
        }
    });
});
Peter Ajtai