Javascript - Simple Filter on Checkbox


SUBMITTED BY: Guest

DATE: Aug. 7, 2013, 3:58 p.m.

FORMAT: JavaScript

SIZE: 2.3 kB

HITS: 1023

  1. <HTML>
  2. <head>
  3. <title></title>
  4. <script type="text/javascript">
  5. function cbChecked() {
  6. hideAll(); //Hide all of the Divs
  7. var cb = document.getElementsByTagName('input');
  8. //Loop through all of the checkboxes and see if they are checked
  9. for (var i=0; i<cb.length; i++)
  10. {
  11. if (cb[i].getAttribute('type') == "checkbox")
  12. {
  13. if (cb[i].checked==true)
  14. {
  15. //If the Current Checkbox is checked, show the div's specific to that one
  16. whatToShow(cb[i].id);
  17. }
  18. }
  19. }
  20. }
  21. function whatToShow(id) {
  22. var d1 = document.getElementById('div1');
  23. var d2 = document.getElementById('div2');
  24. //Display Div's based on the checkbox ID
  25. switch (id) {
  26. case 'ARank':
  27. d1.style.display = 'block';
  28. break;
  29. case 'BRank':
  30. d2.style.display = 'block';
  31. break;
  32. case 'CRank':
  33. d1.style.display = 'block';
  34. d2.style.display = 'block';
  35. break;
  36. default:
  37. break;
  38. }
  39. }
  40. function hideAll() {
  41. //I used a div around all the divs to allow for divs outside that will not be hidden
  42. var parent = document.getElementById('enclosure');
  43. //gets all child divs within the parent div
  44. var child = parent.getElementsByTagName('div');
  45. //loop through and hide all child divs
  46. for (i = 0; i < child.length; i++) {
  47. child[i].style.display = 'none';
  48. }
  49. }
  50. </script>
  51. </head>
  52. <body>
  53. <input type="checkbox" id="ARank" onclick="cbChecked()"> A Rank Jutsu
  54. <input type="checkbox" id="BRank" onclick="cbChecked()"> B Rank Jutsu
  55. <input type="checkbox" id="CRank" onclick="cbChecked()"> C Rank Jutsu
  56. <div id="enclosure">
  57. <div id="div1">ARank BRank weap element</div>
  58. <div id="div2">space weap water</div>
  59. </div>
  60. </body>
  61. </HTML>

comments powered by Disqus