469,346 Members | 6,467 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,346 developers. It's quick & easy.

Works in IE but not in FF - Please Help

I have multiple rows in a table that I can toggle their visibility in IE 6 using the function below. However, this does not work in FF 2.x. I can't use getElementById() because I need to identify multiple <TR> tags in the document.

Can someone please show me how to recode this function below to work in both browsers. TIA.

function hideEmptyFields_ff()
{
var oLink = document.getElementById('hidelink')
//var oImages = document.getElementsByTagName('TR');
var oRows = document.all || document.getElementsByTagName('TR');

if (oRows.length)
{
for (var i = 0; i < oRows.length; i++)
{
var oRow = oRows[i]
if (oRow.name == 'emptyrow')
{
if (oRow.style.display == 'none')
{
oRow.style.display = '';
oLink.innerHTML = 'Hide Empty Fields'
}
else
{
oRow.style.display = 'none';
oLink.innerHTML = 'Show Empty Fields'
}
}
}
}
}
Mar 29 '07 #1
3 1508
acoder
16,027 Expert Mod 8TB
What's the default style setting for the rows?
Mar 29 '07 #2
Not sure what you mean by default setting for the rows. I can tell you that the rows in the table I want to hide are given the following attribute:

name='hiderow'

These are rows that represent empty fields from my data source so I am giving the user an option to hide these empty rows.

Most examples providing an alternative to IE's document.all suggest using getElementById() but this only works for single object in the DOM. Since I want to find multiple rows, I used the name attribute instead where I can have multiple objects with the same name. So I needed to use getElementsByTagName() (notice the plurality of elements in the name) to get a collection of all the rows with this attribute.

After researching the getElementsByTagName(), I was able to construct an alternate function that appears to work in both IE 6 and FF 2.x and is shown below. It appears that assigning an single a single object in the collection to a new variable does not work in FF although this appears like a standard OO contruct. Or maybe I just missed something simple.

var oRow = oRows(iImg);

Here it function that works in both IE 6 and FF 2.x:

function hideEmptyFields_ff()
{
var oLink = document.getElementById('hidelink')
//var oImages = document.getElementsByTagName('TR');
var oRows = document.all || document.getElementsByTagName('TR');

for (var i = 0; i < oRows.length; i++)
{
n = oRows[i].getAttribute("name");
if (n == 'emptyrow')
{
if (oRows[i].style.display == 'none')
{
oRows[i].style.display = '';
oLink.innerHTML = 'Hide Empty Fields'
}
else
{
oRows[i].style.display = 'none';
oLink.innerHTML = 'Show Empty Fields'

}
}
}
}

Does anyone see a problem with this code?

I did see an example where the table is and ID and getElementById() is used to first get a reference to the table and then the table reference is used to get all ('TR') tags. I guess this might improve speed since the initial collection is limited to the table to locate the rows. Perhaps I will try this and post again if the difference it worth noting. TIA.


Finally, an interesting observation is that I can call this function from the <Body> tag's onload event so that the "named" rows are actually hidden before the page first displays. IE 6 instantly displays the page with the rows already hidden while FF displays the page with rows as visible and then after a fraction of second, the rows are hidden which is not desirable. FF apparently displays the page and then runs the function to hide the rows after the fact. I am trying to like FF but in this case, IE 6 appears to handle this in a superior way. Is there a way to have FF not display the page until the rows are hidden?
Mar 29 '07 #3
iam_clint
1,208 Expert 1GB
var oRows = document.getElementsByTagName('TR');
Mar 29 '07 #4

Post your reply

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

Similar topics

2 posts views Thread by James Zhuo | last post: by
1 post views Thread by =?Utf-8?B?amVubmlmZXI=?= | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.