3

I have two fields in my form, I need handle event when browser changes value of them, my fields are username and password; I've used this $('input').on('input change',function(e){//...}), but when I select a user name in list that popped up by my browser, value of password changes but 'input' event of password not working in that case.

How can I handle that ?

Code:

HTML:

<form action="/">
  <input name="username" type="text" />
  <br />
  <input name="password" type="password" />
  <br />
  <input type="submit"/>
</form>

Js:

$('input[type="password"]').on('change keyup',function(event){
  console.log(event.type);
});

It's working when I'm typing in password field, but not working when browser auto complete is changing password value.

Pejman
  • 2,442
  • 4
  • 34
  • 62

2 Answers2

2

This is browser behavior, not about scripting. I think there is no easy way to go around this.

A workaround is to set up a timer to periodically check for changes in the field.

Another workaround is to set up the onfocus to capture the before value and onblur event to capture the after value and compare if the value changes.

If it's crucial to you to handle the event when the field value changes. I think you should disable browser auto complete by specifying autocomplete="off" on the field and optionally implement auto complete yourself.

For more information, see this link.

Community
  • 1
  • 1
Khanh TO
  • 48,509
  • 13
  • 99
  • 115
0

It should be $('input').on('change', function(e){//...}) as documentation says

.on( events [, selector ] [, data ], handler(eventObject) )

you can try with this also:

$('input[type=text], input[type=password]').on('change', function(e){//...})
Daniele
  • 1,938
  • 16
  • 24
  • @OMiD.. Encase you code inside `DOM Ready handler`, if the script is defined inside head element – Sushanth -- Aug 11 '13 at 09:11
  • `$('input[type=text], input[type=password]').on('change', function(e){/*...*/})` not working too, the problem is not selector I think. – Pejman Aug 11 '13 at 09:24
  • try with `.on('change keyup'` instead of `.on('change'` maybe this would help for browser autocomplete – Daniele Aug 11 '13 at 09:31
  • @Daniele still nothing, I did change my username value by auto compelete but browser changes my password value too, i need to handle event when password changes. – Pejman Aug 11 '13 at 09:40