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. this.onOpenChange(openUpdate);
  17. button.textContent = (openUpdate) ? 'показать' : 'скрыть';
  18. input.type = (openUpdate) ? 'password' : 'text';
  19. }
  20. this.getOpen = () => open;
  21. input.addEventListener('input', event => {
  22. if (typeof this.onChange === 'function'){
  23. this.onChange(event.target.value);
  24. }
  25. });
  26. this.onOpenChange = isOpen => isOpen;
  27. }
  28. let p = new Password(document.body, true);
  29. p.onChange = data => console.log(data);
  30. p.onOpenChange = open => console.log(open);
  31. p.setValue('qwerty');
  32. console.log(p.getValue());
  33. p.setOpen(false);
  34. console.log(p.getOpen());