简体   繁体   中英

How do I change the text of a password element input JavaScript

I want programmatically input password with JavaScript. I'm use this code

document.getElementById('password').value='Hh1234567';

When I click the submit button, it shows an error: "password is required please input password". But when input password with manually with the keybord it works fine.

How can I make it accept my password entered via the script?

Simply setting the value of the password field doesn't alone create the event necessary for the change of that value to be detected, so you need something like this:

  const pwd = document.getElementById('password');
  pwd.value = 'Hh1234567';
  pwd.dispatchEvent(new Event('input', { bubbles: true, cancelable: true }));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM