test.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. function Password(parent, open = false) {
  2. let inpPass = document.createElement("input");
  3. let checkBox = document.createElement("input");
  4. checkBox.setAttribute("type", "checkbox");
  5. if (open) checkBox.setAttribute("checked", "checked");
  6. let text = document.createElement("span");
  7. text.innerText = "view";
  8. this.setValue = function (value) {
  9. inpPass.value = value;
  10. };
  11. this.getValue = function () {
  12. return inpPass.value;
  13. };
  14. var test = () =>
  15. checkBox.checked
  16. ? (inpPass.type = "text")
  17. : (inpPass.type = "password");
  18. this.setOpen = function () {
  19. checkBox.checked;
  20. test();
  21. };
  22. this.getOpen = function () {
  23. return checkBox.checked;
  24. };
  25. inpPass.oninput = () => {
  26. this.onChange();
  27. };
  28. checkBox.onclick = () => {
  29. test();
  30. this.onOpenChange(checkBox.checked);
  31. };
  32. parent.append(inpPass);
  33. parent.append(checkBox);
  34. parent.append(text);
  35. }
  36. let p = new Password(document.body, true);
  37. p.onOpenChange = (open) => console.log(open);