index.html 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <script>
  11. function Password(parent, open) {
  12. const input = document.createElement('input');
  13. input.type = 'password';
  14. parent.appendChild(input);
  15. const button = document.createElement('button');
  16. button.type = 'button';
  17. button.textContent = 'показать';
  18. parent.appendChild(button);
  19. button.addEventListener('click', () => {
  20. this.setOpen(open !== true);
  21. });
  22. this.setValue = newValue => input.value = newValue;
  23. this.getValue = () => input.value;
  24. this.setOpen = openUpdate => {
  25. open = openUpdate;
  26. if(typeof this.onOpenChange === 'function') {
  27. this.onOpenChange(openUpdate);
  28. }
  29. button.textContent = (openUpdate) ? 'показать' : 'скрыть';
  30. input.type = (openUpdate) ? 'password' : 'text';
  31. }
  32. this.getOpen = () => open;
  33. input.addEventListener('input', event => {
  34. if (typeof this.onChange === 'function'){
  35. this.onChange(event.target.value);
  36. }
  37. });
  38. }
  39. let p = new Password(document.body, true);
  40. p.onChange = data => console.log(data);
  41. p.onOpenChange = open => console.log(open);
  42. p.setValue('qwerty');
  43. console.log(p.getValue());
  44. p.setOpen(false);
  45. console.log(p.getOpen());
  46. </script>
  47. </body>
  48. </html>