036-form-events.html 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>036 form events</title>
  6. <style>
  7. html, body { margin: 0; padding: 0; background: #ffffff; color: #202020; }
  8. body { font-family: sans-serif; font-size: 13px; padding: 10px; }
  9. h1 { font-size: 20px; margin: 0 0 6px 0; }
  10. p { margin: 0 0 10px 0; }
  11. .row { margin: 6px 0; }
  12. label { margin-right: 8px; }
  13. input, select, textarea, button { font-family: sans-serif; font-size: 13px; }
  14. #result { margin-top: 10px; padding: 8px; background: #eeeeee; color: #000000; font-family: monospace; font-weight: bold; }
  15. </style>
  16. </head>
  17. <body>
  18. <h1>036 &middot; form events and elements</h1>
  19. <p>Listeners must bubble to document, <code>form.elements</code> must list the controls, and a cancelled click/submit must block the default action.</p>
  20. <form id="f" action="/nowhere.html">
  21. <div class="row">
  22. <label for="alpha">Alpha</label><input id="alpha" name="alpha" value="one">
  23. <label><input id="beta" name="beta" type="checkbox"> Beta</label>
  24. </div>
  25. <div class="row">
  26. <button id="go" type="submit" name="send" value="yes">Go</button>
  27. </div>
  28. </form>
  29. <div id="result">checking...</div>
  30. <script>
  31. setTimeout(function () {
  32. var f = document.getElementById('f');
  33. var a = document.getElementById('alpha');
  34. var b = document.getElementById('beta');
  35. var go = document.getElementById('go');
  36. var out = document.getElementById('result');
  37. var inputs = 0, clicks = 0, docClicks = 0, submits = 0, changes = 0;
  38. var details = [];
  39. a.addEventListener('click', function (e) { clicks++; e.preventDefault(); });
  40. a.addEventListener('input', function (e) {
  41. inputs += (e.target === a) ? 1 : 0;
  42. });
  43. a.addEventListener('change', function () { changes++; });
  44. f.addEventListener('submit', function (e) { submits++; e.preventDefault(); });
  45. document.addEventListener('click', function (e) {
  46. if (e.target === a) docClicks++;
  47. });
  48. a.dispatchEvent(new Event('input', { bubbles: true }));
  49. a.value = 'two';
  50. a.click();
  51. go.click();
  52. var ok = true;
  53. var elems = f.elements;
  54. if (elems.length !== 3) { ok = false; details.push('length=' + elems.length); }
  55. if (elems.alpha !== a) { ok = false; details.push('named element mismatch'); }
  56. if (elems[0] !== a) { ok = false; details.push('indexed element mismatch'); }
  57. if (inputs !== 1) { ok = false; details.push('input=' + inputs); }
  58. if (clicks !== 1) { ok = false; details.push('click=' + clicks); }
  59. if (docClicks !== 1) { ok = false; details.push('document click=' + docClicks); }
  60. if (submits !== 1) { ok = false; details.push('submit=' + submits); }
  61. if (a.value !== 'two') { ok = false; details.push('value=' + a.value); }
  62. b.checked = true;
  63. f.reset();
  64. if (a.value !== 'one' || b.checked !== false) {
  65. ok = false;
  66. details.push('reset value=' + a.value + ' checked=' + b.checked);
  67. }
  68. var removed = 0;
  69. function once() { removed++; }
  70. a.addEventListener('change', once);
  71. a.removeEventListener('change', once);
  72. a.dispatchEvent(new Event('change', { bubbles: true }));
  73. if (removed !== 0) { ok = false; details.push('removed listener ran=' + removed); }
  74. if (changes !== 1) { ok = false; details.push('change=' + changes); }
  75. var ceTarget = false;
  76. a.addEventListener('change', function (e) { ceTarget = (e.target === a); });
  77. var ce = document.createEvent('Event');
  78. ce.initEvent('change', true, false);
  79. a.dispatchEvent(ce);
  80. if (!ceTarget) { ok = false; details.push('createEvent target'); }
  81. if (changes !== 2) { ok = false; details.push('createEvent change=' + changes); }
  82. out.textContent = ok ? 'PASS: form events and elements' :
  83. ('FAIL: ' + details.join(', '));
  84. out.style.background = ok ? '#ccffcc' : '#ffcccc';
  85. }, 600);
  86. </script>
  87. </body>
  88. </html>