I have a table of items being pulled from a database. The items are laid out as follows:
(+) 02/04/2014
Item 1
(+) 02/05/2014
Item 2
Item 3
(+) 02/06/2014
Item 4
Comment for item 4
Item 5
The items under the dates are hidden, when the user clicks the (+) sign, it will show the items below it and replace the (+) with a (-) to hide the items.
The HTML roughly looks as follows:
<table>
<tr><td>(+)</td><td>02/04/2014</td></tr>
<tr id="id_1" class="hidden"><td colspan="2">Item 1</td></tr>
<tr><td>(+)</td><td>02/05/2014</td></tr>
<tr id="id_2" class="hidden"><td colspan="2">Item 2</td></tr>
<tr id="id_3" class="hidden"><td colspan="2">Item 2</td></tr>
<tr><td>(+)</td><td>02/06/2014</td></tr>
<tr id="id_4" class="hidden"><td colspan="2">Item 4</td></tr>
<tr id="comment_id_4" class="hidden"><td colspan="2">Comment for item 4</td></tr>
<tr id="id_5" class="hidden"><td colspan="2">Item 5</td></tr>
</table>
The only way I've been able to think of doing it is by dynamically generating a JavaScript function by looping through the records again and adding this a href to the plus sign:
<a href="#" onclick="toggle(242014);return false">(+)</a>
And here's a sample of the generated JavaScript:
function toggle(id) {
switch (id) {
case 242014:
$("#id_1").toggle();
break;
case 252014:
$("#id_2").toggle();
$("#id_3").toggle();
break;
case 262014;
$("#id_4").toggle();
$("#comment_id_4").toggle();
$("#id_5").toggle();
break;
}
It works, but it's ugly. Is there a more elegant way of doing this?
Thanks.