code_junkie

Modify javascript to work without form

Nov 13th, 2011
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. <script language="javascript">
  2.  
  3. // Set the default "show" mode to that specified by W3C DOM
  4. // compliant browsers
  5.  
  6. var showMode = 'table-cell';
  7.  
  8. // However, IE5 at least does not render table cells correctly
  9. // using the style 'table-cell', but does when the style 'block'
  10. // is used, so handle this
  11.  
  12. if (document.all) showMode='block';
  13.  
  14. // This is the function that actually does the manipulation
  15.  
  16. var States = { };
  17.  
  18. function toggleVis(col){
  19.  
  20. if (!States[col] || States[col].IsOpen == null)
  21. {
  22. States[col] = {isOpen : true}; // This assumes the cell is already shown
  23. }
  24.  
  25. mode = States[col].IsOpen ? showMode : 'none';
  26.  
  27. cells = document.getElementsByName(col);
  28. for(j = 0; j < cells.length; j++) cells[j].style.display = mode;
  29.  
  30. States[col].IsOpen = !States[col].IsOpen;
  31. }
  32.  
  33. </script>
  34.  
  35. <a href="#" onclick="toggleVis('tcol1'); return false;">Toggle 1</a>
  36. <a href="#" onclick="toggleVis('tcol2'); return false;">Toggle 2</a>
  37. <a href="#" onclick="toggleVis('tcol3'); return false;">Toggle 3</a>
  38. <a href="#" onclick="toggleVis('tcol4'); return false;">Toggle 3</a>
  39.  
  40. <table border=1>
  41. <tr>
  42. <td name="tcol1" class="bold">column 1 text</td>
  43. <td name="tcol2" >column 2 text</td>
  44. <td name="tcol3" class="italic">column 3 text</td>
  45. <td name="tcol4" >column 4 text</td>
  46. </tr>
  47. <tr>
  48. <td name="tcol1" class="bold">column 1 text</td>
  49. <td name="tcol2" >column 2 text</td>
  50. <td name="tcol3" class="italic">column 3 text</td>
  51. <td name="tcol4" >column 4 text</td>
  52. </tr>
  53. </table>
  54.  
  55. mode = States[col].IsOpen ? showMode : 'none'; <--Set mode to current state opposite of current(shown or hidden)
  56.  
  57. cells = document.getElementsByName(col); <--Object of specified col
  58. for(j = 0; j < cells.length; j++) cells[j].style.display = mode; <--Set show/hide for each col
  59.  
  60. States[col].IsOpen = !States[col].IsOpen; <-- Not sure what this does?
Add Comment
Please, Sign In to add comment