Alex Rivera | Logout

jQuery trigger onChange

Asked 2012-02-05T16:44:48.650
15

I have a input text field which is filled with the url of an image using a jQuery function. How do I use the "onChange" event of the input to run the jQuery function?

Edit
Report

1 Answer

0

I can't imagine the problem lies with .change() perhaps somewhere else in your code, I have just tested and the event will get fired if I copy and paste, or if I type some new text (with both normal and onscreen keyboard).

The only time it didn't seem to trigger was when I dragged some text clipping from the desktop to the input box but this was to do with it being highlighted and sure enough when the input box lost focus the event triggered (I can't think of any other use cases)

You can do something like below using the blur() or focusout() event which essentially I believe is the change event

function foo(e) {
    alert(e.type + ' triggered');
}

$('input').blur(function(e) {
    $t = $(this);
    if ($t.data('oVal') != $t.val()) {
        $t.data('oVal', $t.val());
        foo(e);
    }
})
answered 2012-02-05T17:28:18.470

Your Answer