0

Why doesn't the input event get fired when I changed a text input's value via jQuery.val('')?

http://jsfiddle.net/EC85C/2/

https://developer.mozilla.org/en-US/docs/Web/Reference/Events/input

3
  • 3
    any of the events like change, blur, input etc are fired only when a direct user action causes that change Commented Feb 21, 2014 at 2:23
  • Even't aren't fired by jQuery. It's the browser you're using that doesn't have support for the input event Commented Feb 21, 2014 at 2:45
  • You need to trigger that change: $('.selector').trigger('change'); etc Commented Feb 21, 2014 at 2:56

1 Answer 1

1

My guess is you're trying to get AngularJS to see your changes (via bindings) when you change the value of your input (through jQuery's val method)?

I've thrown together this fiddle to illustrate how to get this to work: http://jsfiddle.net/6JTDf/1/

Instead of calling trigger (which creates a jQuery 'event'), you can create a synthetic UIEvent (in our case, 'input'), and then dispatch that on the element. This synthetic event will trigger both the event listener (hello alert!), and the angular binding.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. I guess it's just a pitfall in using jquery plugins with angular hu? I figured there should/would be more support since jquery actually does have quite a few legit libraries. If I want to use jquery libraries in the future for this sort of thing, am I really always going to have to edit the plugin so that it also fires the input event in order to update the model?
I think you're right about the pitfall. I'm guessing there's an "Angular" way of updating the value programmatically, which in turn updates the model as well. I've read articles where they warn against using jQuery outside of Angular, probably for this reason. Good luck!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.