Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-04-2009, 10:01 AM   PM User | #1
stjörnubörnum
New to the CF scene

 
Join Date: Nov 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
stjörnubörnum is an unknown quantity at this point
How do I make this script expand / collapse / expand table headers?

Hello everyone, I am just learning the basics of javascript, and am finding myself stuck. Any help is greatly appreciated.

So, I'm using the below javascript for expand / collapse text, it suffices well. However it will only collapse either table rows, or table data cells. Now, I know that these tags

Code:

{var trs = table.getElementsByTagName('tr');
{var a = trs[i].getElementsByTagName('td')
Make the rows / data cells expand. However, I cannot seem to make it work on headers.

To be lucid, I would like to know what I have to add, or edit to make the below code expand table headers (<th>), and perhaps div tags. The full javascript is thus :

Code:

window.onload = function()
{
var table = document.getElementById('stjorn');
if (table)
{
var trs = table.getElementsByTagName('tr');
for(var i = 0; i < trs.length; i++) 
{
var a = trs[i].getElementsByTagName('td')[0].getElementsByTagName('a')[0];
a.onclick = function()
{
var span = this.parentNode.getElementsByTagName('span')[0];
span.style.display = span.style.display == 'none' ? 'block' : 'none';
this.firstChild.nodeValue = span.style.display == 'none' ? 'More' : 'Less';};
}
}
};
Thank you for your time.
stjörnubörnum is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:11 AM.


Advertisement
Log in to turn off these ads.