473,398 Members | 2,403 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,398 developers and data experts.

Expand / Collapse FIELDSET

1
I wrote simple code for expandable/collapsable fieldsets. You can try it yourselves:

Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  5. <title>Test</title>
  6.  
  7. <style type="text/css">
  8. <!--
  9. a { color:blue; cursor:pointer; }
  10. -->
  11. </style>
  12. <script type="text/javascript">
  13. function constructCollapsableFieldsets() 
  14. {
  15.     var allFsets = document.getElementsByTagName('fieldset');
  16.     var fset = null;
  17.     for (var i=0; i<allFsets.length; i++)
  18.     {
  19.         fset = allFsets[i];
  20.         if(fset.attributes['collapsed']!=null)
  21.             constructCollapsableFieldset(fset, fset.attributes['collapsed'].value);
  22.     }
  23. }
  24.  
  25. //for collapsable fieldset:
  26. function constructCollapsableFieldset(fset, collapsed)
  27. {
  28.     //main content:
  29.     var divContent = fset.getElementsByTagName('div')[0];
  30.     if (divContent == null)
  31.         return;
  32.  
  33.     if (collapsed == 'true')
  34.         divContent.style.display = 'none';
  35.  
  36.     //+/- ahref:
  37.     var ahrefText = getAHrefForToogle(collapsed);
  38.  
  39.     //legend:
  40.     var legend = fset.getElementsByTagName('legend')[0];
  41.     if (legend != null)
  42.         legend.innerHTML = ahrefText + legend.innerHTML;
  43.     else
  44.         fset.innerHTML = '<legend>' + ahrefText + '</legend>' + fset.innerHTML;
  45. }
  46.  
  47. function getAHrefForToogle(collapsed)
  48. {
  49.     var ahrefText = "<a onClick='toogleFieldset(this.parentNode.parentNode);' style='text-decoration: none;'>";
  50.     ahrefText = ahrefText + getExpanderItem(collapsed) + "</a>&nbsp;";
  51.     return ahrefText;
  52. }
  53.  
  54. function getExpanderItem(collapsed)
  55. {
  56.     var ecChar;
  57.     if (collapsed=='true')
  58.         ecChar='+';
  59.     else
  60.         ecChar='-';
  61.  
  62.     return ecChar;
  63. }
  64.  
  65. function toogleFieldset(fset)
  66. {
  67.     var ahref = fset.getElementsByTagName('a')[0];
  68.     var div = fset.getElementsByTagName('div')[0];
  69.  
  70.     if (div.style.display != "none")
  71.     {
  72.         ahref.innerHTML=getExpanderItem('true');
  73.         div.style.display = 'none';
  74.     }
  75.     else
  76.     {
  77.         ahref.innerHTML=getExpanderItem('false');
  78.         div.style.display = '';
  79.     }
  80. }
  81. //-->
  82. </script>
  83. </head>
  84.  
  85. <body onLoad="constructCollapsableFieldsets();">
  86.  
  87. <fieldset collapsed="true">
  88.     <legend>Bububu</legend>
  89.     <div>
  90.         This is collapsable fieldset with caption (legend).
  91.         Fieldset is initially collapsed.
  92.     </div>
  93. </fieldset>
  94. <br />
  95. <fieldset collapsed="false">
  96.     <div>
  97.         This is collapsable fieldset with no caption (legend).
  98.         Fieldset is initially expanded.
  99.     </div>
  100. </fieldset>
  101. <br />
  102. <fieldset>
  103. <legend>Look here</legend>
  104. <div>
  105.     This fieldset is not collapsable.. 
  106.     To set it collapseble is needed to type
  107.     &lt;fieldset collapsed="false" ...
  108.     or &lt;fieldset collapsed="true" ...
  109. </div>
  110. </fieldset>
  111. </body>
  112. </html>
  113.  
Oct 10 '08 #1
1 16371
Not bad. A few hints if I may:

* try using classes, not attributes. This will allowed the document to be validated for HTML/javascript. eg class="fieldset_collapsed" : )

* try using unobtrusive javascript, a seperate .JS file with all functions in one object.

* if there is no inner DIV, let your javascript create it, instead of returning. Saves the programmer from having to add a silly DIV each time. Or just always create one, if that's easier.

Nice work tho!
Apr 30 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: James Hurrell | last post by:
Hi, I'm using the following javascript function to expand and collapse portions of text in a web page (targeted at IE5.5 and above): function doExpand(paraNum,arrowNum) { if...
7
by: peter | last post by:
I use the click-to-expand menu at http://javascript.internet.com/navigation/click-to-expand-menu.html This works fine, but is there a way to expand or collapse all the menus? An example of how...
1
by: Randy Starkey | last post by:
Hi, Is there a way to expand and collapse all if I have multiple implementations of this script on a single page? The script works well individually. Thanks! --Randy Starkey ---
2
by: Jenny | last post by:
My codebehind file contains a lot of sub's. Is there a fast way to expand and collapse them in the VisualStudio? It's really exhausting to collapse them all after opening the project in the...
4
by: Gönen EREN | last post by:
how can i collapse or expand a node of treeview control programmaticly? thanks.
0
by: Shadow Lynx | last post by:
When using ASP.NET 2.0's built-in TreeView on a page with <BASE target = "AnythingBut_Self"></BASE> in the HEAD, the expand/collapse buttons fail to function. The reason for this is that the...
2
by: sam | last post by:
Hi all, I have a series of divs in 2 blocks say BLOCK1 and BLOCK2 and I want to use one click to expand/collapse all those in each block. But the code I came up with exapands all the divs in the...
0
by: jim | last post by:
Hi, I have a TreeView control that sits on the MasterPage. All of my other webpages inherit from that Master Page. The Treeview receives it's data using an XMLDataSource that has it's DataFile...
4
by: Rabel | last post by:
I am not very good at javascript I mostly am a flash developer but I am trying to apply one of our old expanding menus to work for a new site but it doesn't collapse the way I need it to right now...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.