Js-HW10.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. function Password(parent, open) {
  2. let value = ''
  3. let pass = document.createElement('input')
  4. pass.placeholder = 'enter password'
  5. let showPassBox = document.createElement('div')
  6. let check = document.createElement('input')
  7. check.type = 'checkbox'
  8. check.name = 'password'
  9. let label = document.createElement('label')
  10. label.for = 'password'
  11. label.innerText = 'Show password'
  12. showPassBox.append(check, label)
  13. parent.append(pass, showPassBox)
  14. check.onchange = () => {
  15. this.setOpen(check.checked)
  16. }
  17. pass.oninput = () => {
  18. if (typeof this.onChange === "function") {
  19. this.onChange(this.getValue());
  20. }
  21. };
  22. this.getValue = function () {
  23. return pass.value
  24. }
  25. this.setValue = function(value) {
  26. pass.value = value
  27. }
  28. this.getOpen = function () {
  29. return open
  30. }
  31. this.setOpen = function(newOpen) {
  32. open = newOpen
  33. pass.type = open ? 'text' : 'password'
  34. check.checked = open
  35. if(typeof this.onOpenChange === 'function') {
  36. this.onOpenChange(open)
  37. }
  38. }
  39. this.setOpen(open)
  40. this.onChange = () => {
  41. button.disabled = !(p.getValue() === p2.getValue())
  42. }
  43. }
  44. // let p = new Password(document.body, false)
  45. //
  46. // p.onChange = data => console.log(data)
  47. // p.onOpenChange = open => console.log(open)
  48. //
  49. // p.setValue('qwerty')
  50. // console.log(p.getValue())
  51. // p.setOpen(false)
  52. // console.log(p.getOpen())
  53. function Login(parent) {
  54. let input = document.createElement('input')
  55. input.placeholder = 'enter login'
  56. let button = document.createElement('button')
  57. button.innerText = 'log in'
  58. parent.appendChild(input)
  59. let password = new Password(parent, false)
  60. password.placeholder = 'enter password'
  61. button.disabled = true
  62. parent.appendChild(button)
  63. parent.onchange = function () {
  64. console.log(password.getValue())
  65. button.disabled = !(input.value !== '' && password.getValue() !== '')
  66. }
  67. }
  68. let newLogin = new Login(loginForm)
  69. console.log(newLogin)
  70. let p = new Password(document.body, true)
  71. let p2 = new Password(document.body, true)
  72. let button = document.createElement('button')
  73. button.innerText = 'log in'
  74. button.disabled = true
  75. document.body.append(button)