password.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. function Password(parent, open) {
  2. const input = document.createElement('input');
  3. input.type = 'password';
  4. parent.appendChild(input);
  5. const button = document.createElement('button');
  6. button.type = 'button';
  7. button.textContent = 'показать';
  8. parent.appendChild(button);
  9. button.addEventListener('click', () => {
  10. this.setOpen(open !== true);
  11. });
  12. this.setValue = newValue => input.value = newValue;
  13. this.getValue = () => input.value;
  14. this.setOpen = openUpdate => {
  15. open = openUpdate;
  16. if(typeof this.onOpenChange === 'function') {
  17. this.onOpenChange(openUpdate);
  18. }
  19. button.textContent = (openUpdate) ? 'показать' : 'скрыть';
  20. input.type = (openUpdate) ? 'password' : 'text';
  21. }
  22. this.getOpen = () => open;
  23. input.addEventListener('input', event => {
  24. if (typeof this.onChange === 'function'){
  25. this.onChange(event.target.value);
  26. }
  27. });
  28. }
  29. let p = new Password(document.body, true);
  30. p.onChange = data => console.log(data);
  31. p.onOpenChange = open => console.log(open);
  32. p.setValue('qwerty');
  33. console.log(p.getValue());
  34. p.setOpen(false);
  35. console.log(p.getOpen());