JQuery trigger onChange

I have an input text field that is populated with the image url using the jQuery function. How to use onChange event for input to run jQuery function?

+6
source share
4 answers

If you update the field in JavaScript, then after changing the value, just call its change()

 $('#myId').val('new_url').change(); 
+6
source

try the following:

 $("#myId").trigger('event_name') 

where event_name is the event that you associated with your validation function, for example keyUp or change

+6
source

Try it -

 $('#myId').trigger('change'); 

Put it in any function where you change the url in the text box.

+6
source

I canโ€™t imagine that the problem is .change() , maybe somewhere else in your code, I just tested and the event will fire if I copy and paste, or if I type new text (with both normal and with on-screen keyboard).

The only time it didnโ€™t work was when I dragged a piece of text from the desktop to the input window, but this was due to the fact that it was highlighted and quite sure when the input field lost focus, the event triggered (I canโ€™t think any other use cases)

You can do something like below using the blur() or focusout() event, which in essence I consider 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); } }) 
0
source

Source: https://habr.com/ru/post/907706/


All Articles