473,465 Members | 2,092 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

two collapsible tables, one toggle function, much frustration

1 New Member
Hello. I'm painfully new to this and am having trouble figuring out what I'm doing wrong... I have two small tables in a simple html file. I want each table to have the ability to collapse/expand when the user clicks selected text. I've attempted the javascript toggle function. I believe the problem is somewhere in how I'm calling the function, but I dont know exact what...

Any help would be much appreciated! Thanks!


[HTML]<html>
<head>
<script>
function toggle()
{
if(document.getElementById().style.display=='none' )
{document.getElementById().style.display = '';
}
else
{document.getElementById('ms').style.display = 'none';
}
}
</script>
</head>

<span onClick="toggle("ms");">-/+ </span>
<strong><font color="#0000ff">Product</font></strong>
<table id="ms" cellspacing="0" cellpadding="10" border="0">

<tr valign="top">
<td></td>
<td width="250">ONE</td>
<td>A</td>
</tr>

<tr valign="top">
<td></td>
<td>TWO</td>
<td>B</td>
</tr>

<tr valign="top">
<td></td>
<td>THREE</td>
<td>C</td>
</tr>

</table><br>

<span onClick="toggle("cat");">-/+ </span>
<strong><font color="#0000ff">Category</font></strong>
<table id="cat" cellspacing="0" cellpadding="10" border="0">

<tr valign="top">
<td></td>
<td width="250">FOUR</td>
<td>D</td>
</tr>

<tr valign="top">
<td></td>
<td>FIVE</td>
<td>E</td>
</tr>

<tr valign="top">
<td></td>
<td>SIX</td>
<td>F</td>
</tr>

</table><br>
</body>
</html>[/HTML]
Sep 22 '06 #1
1 5215
acoder
16,027 Recognized Expert Moderator MVP
Four problems:
  1. When calling toggle in the span onclick, you're using double quotes within double quotes.
  2. The toggle function requires an argument to capture the passed id string
  3. document.getElementById needs an id to get an element.
  4. Use the passed id for the else part otherwise it will collapse the wrong table
Here's the modified code:
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script>
  4. function toggle(id)
  5. {
  6. if(document.getElementById(id).style.display=='none' )
  7. {document.getElementById(id).style.display = '';
  8. }
  9. else
  10. {document.getElementById(id).style.display = 'none';
  11. }
  12. }
  13. </script>
  14. </head>
  15.  
  16. <span onclick="toggle('ms');">-/+ </span>
  17. <strong><font color="#0000ff">Product</font></strong>
  18. <table id="ms" cellspacing="0" cellpadding="10" border="0">
  19.  
  20. <tr valign="top">
  21. <td></td>
  22. <td width="250">ONE</td>
  23. <td>A</td>
  24. </tr>
  25.  
  26. <tr valign="top">
  27. <td></td>
  28. <td>TWO</td>
  29. <td>B</td>
  30. </tr>
  31.  
  32. <tr valign="top">
  33. <td></td>
  34. <td>THREE</td>
  35. <td>C</td>
  36. </tr>
  37.  
  38. </table><br>
  39.  
  40. <span onclick="toggle('cat');">-/+ </span>
  41. <strong><font color="#0000ff">Category</font></strong>
  42. <table id="cat" cellspacing="0" cellpadding="10" border="0">
  43.  
  44. <tr valign="top">
  45. <td></td>
  46. <td width="250">FOUR</td>
  47. <td>D</td>
  48. </tr>
  49.  
  50. <tr valign="top">
  51. <td></td>
  52. <td>FIVE</td>
  53. <td>E</td>
  54. </tr>
  55.  
  56. <tr valign="top">
  57. <td></td>
  58. <td>SIX</td>
  59. <td>F</td>
  60. </tr>
  61.  
  62. </table><br>
  63. </body>
  64. </html>
May 17 '08 #2

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

Similar topics

1
by: Bootstrap bill | last post by:
I'm looking to create collapsible tables, similar to the outlines available in Microsoft Excel and Open Office spreadsheet. I'd prefer something cross platform. Is something like this available...
5
by: Steven | last post by:
Can anyone tell me how to toggle the "Caps Lock" key? Thanks in advance
1
by: mht7 | last post by:
I'm newbie to javascript and I did an extensive search on this site and couple of others looking for directions. I'm attempting to write some custom javascript for collapsing the tables and fit it...
5
by: Jonathan | last post by:
Hello All, I am in the process of creating a 2/3 level collapsible/exspanible menu (called "nav") where users can click on a category and have the submenus appear beneath it and so on (allow...
7
by: Kamal | last post by:
Hello all, I have a very simple html table with collapsible rows and sorting capabilities. The collapsible row is hidden with css rule (display:none). When one clicks in the left of the...
5
by: ting ting | last post by:
I am working on a ASP.net 2.0 version and I want to click a button to open a window. I was using window.open to write at vb side. Response.Write("<Script...
1
by: swiftouch | last post by:
I'm getting an error message in FF2.0: document.getElementById(toggle) has no properties The goal of the script is, when I hover my mouse over an image, to make one div element visible while...
1
by: mahesh123 | last post by:
I am using Collapsible Panel in my project.But in other form of my project i want to use collapsible panel inside other Collapsible panel.Is it possible. Both panels are to be dinamic.Is it possible...
4
by: dagabe14 | last post by:
Hi, this is my first time posting here, so be please be nice. If i do something incorrectly (and admin notices), please let me know of my mistake. So, I am attempting to make nested tables that...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.