Guest User

Untitled

a guest
Jun 22nd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. var checkboxes = slice(document.querySelectorAll('.checkbox'));
  2. for (var checkbox of checkboxes)
  3. checkbox.logic = new Checkbox(checkbox);
  4.  
  5. <div class="demo">
  6.  
  7. <h2>Custom checkboxes</h2>
  8.  
  9. <div tabindex="0" class="checkbox" checked>
  10. Tim-Tams
  11. </div>
  12. <div tabindex="0" class="checkbox">
  13. Mint slices
  14. </div>
  15.  
  16. </div>
  17.  
  18. function Checkbox(el) {
  19. this.el = el;
  20.  
  21. this.el.addEventListener('keydown', this.handleKeyDown.bind(this));
  22. this.el.addEventListener('click', this.toggle.bind(this));
  23.  
  24. // Initialize role and aria-checked state.
  25. this.el.setAttribute('role', 'checkbox');
  26. if (this.el.hasAttribute('checked')) {
  27. this.el.setAttribute('aria-checked', 'true');
  28. } else {
  29. this.el.setAttribute('aria-checked', 'false');
  30. }
  31. }
  32.  
  33. Checkbox.prototype.handleKeyDown = function(e) {
  34. switch(e.keyCode) {
  35. case VK_ENTER:
  36. case VK_SPACE: {
  37. this.toggle();
  38. break;
  39. }
  40. }
  41. };
  42.  
  43. Checkbox.prototype.toggle = function() {
  44. if (this.el.hasAttribute('checked')) {
  45. this.el.removeAttribute('checked');
  46.  
  47. // Keep checked attribute and aria-checked in sync.
  48. this.el.setAttribute('aria-checked', 'false');
  49. } else {
  50. this.el.setAttribute('checked', '');
  51.  
  52. // Keep checked attribute and aria-checked in sync.
  53. this.el.setAttribute('aria-checked', 'true');
  54. }
  55. };
  56.  
  57. //HERE IS THE QUESTION PART================================
  58. var checkboxes = slice(document.querySelectorAll('.checkbox'));
  59. for (var checkbox of checkboxes)
  60. checkbox.logic = new Checkbox(checkbox);
  61.  
  62. var checkboxes = slice(document.querySelectorAll('.checkbox'));
  63. for (var checkbox of checkboxes)
  64. checkbox.logic = new Checkbox(checkbox);
  65.  
  66. var checkbox = {logic: "doing stuff with the Checkbox class that was called"};
Add Comment
Please, Sign In to add comment