Advertisement
thetenfold

Untitled

Sep 11th, 2013
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <title> Date Range Checks </title>
  6.  
  7. <style type="text/css">
  8. #debug {
  9. display: inline-block;
  10. width: 400px;
  11. height: 200px;
  12. padding: 6px;
  13. margin: 20px auto 0 auto;
  14. vertical-align: center;
  15. text-align: center;
  16. font-size: 13pt;
  17. font-family: consolas, courier, serif;
  18. font-weight: normal;
  19. overflow: auto;
  20. border: 1px solid #DDDDDD;
  21. }
  22.  
  23. #limitDateDisplay label {
  24. margin-right: 6px;
  25. padding: 2px;
  26. }
  27. </style>
  28.  
  29. <script type="text/javascript">
  30. 'use strict';
  31.  
  32. var boxes, limitYear, debug;
  33.  
  34. function $(id) {
  35. return document.getElementById(id);
  36. }
  37.  
  38. function uncheck(box) {
  39. box.checked = false;
  40. }
  41.  
  42. function isChecked(box) {
  43. return box.checked;
  44. }
  45.  
  46. function getBoxAndYear(box) {
  47. return box.value + '-' + limitYear.value;
  48. }
  49.  
  50. function clearDateRange() {
  51. [].forEach.call(boxes, uncheck);
  52. }
  53.  
  54. function printDebug() {
  55. debug.textContent = [].filter.call(boxes, isChecked).map(getBoxAndYear).join('\n');
  56. }
  57.  
  58. window.onload = function() {
  59. // using global variables here, so we don't have to call
  60. // getElementsByClassName/getElementById more than once
  61. boxes = document.getElementsByClassName('box');
  62. limitYear = $('limitYear');
  63. debug = $('debug');
  64.  
  65. limitYear.value = new Date().getFullYear();
  66. printDebug();
  67.  
  68. $('clear').addEventListener('click', function () {
  69. clearDateRange();
  70. printDebug();
  71. }, false);
  72.  
  73. // use only one event listener with delegation, to listen
  74. // to all the checkboxes at once, and print the debug for each change
  75. $('limitDateDisplay').addEventListener('change', printDebug, false);
  76. };
  77. </script>
  78.  
  79. </head>
  80.  
  81. <body>
  82.  
  83. <div id="limitDateDisplay">
  84. Limit to month(s): <button id="clear">Clear</button> <br>
  85. <label> <input type="checkbox" value="01" class="box">Jan</label>
  86. <label> <input type="checkbox" value="02" class="box">Feb</label>
  87. <label> <input type="checkbox" value="03" class="box">Mar</label>
  88. <label> <input type="checkbox" value="04" class="box">Apr</label>
  89. <label> <input type="checkbox" value="05" class="box">May</label>
  90. <label> <input type="checkbox" value="06" class="box">Jun</label>
  91. <label> <input type="checkbox" value="07" class="box">Jul</label>
  92. <label> <input type="checkbox" value="08" class="box">Aug</label>
  93. <label> <input type="checkbox" value="09" class="box">Sep</label>
  94. <label> <input type="checkbox" value="10" class="box">Oct</label>
  95. <label> <input type="checkbox" value="11" class="box">Nov</label>
  96. <label> <input type="checkbox" value="12" class="box">Dec</label>
  97. <input type="text" value="" id="limitYear" size="5">
  98. </div>
  99.  
  100. <pre id="debug"></pre>
  101.  
  102. </body>
  103. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement