Hiding function works on IE, but it doesn't on Moziila

A

atomo

Hi, i'm wondering why this hiding and showing function works on IE (6) but
it doesn't work in Mozilla Firebird (0.7)

function fnc_show(num){

for(i=1;i<7;i++){
eval("t_" + i + ".style.display = \"none\"");
}
eval("t_" + num + ".style.display = \"inline\"");

}

t_1, t_2 ...t_7 are the ids for TR elements in a table, i.e. with
fnc_show(3), it will show only t_3. This works without a problem on IE, but
it seems to do nothing in Mozilla, i only get an error telling that t_1 is
not defined.

I'm wondering too, if there is a better way to do it, cause i read that eval
function is inefficient .

Any ideas?
 
V

Vjekoslav Begovic

atomo said:
Hi, i'm wondering why this hiding and showing function works on IE (6) but
it doesn't work in Mozilla Firebird (0.7)

function fnc_show(num){

for(i=1;i<7;i++){
eval("t_" + i + ".style.display = \"none\"");

Use: document.getElementById("t_"+i).display="none";
}
eval("t_" + num + ".style.display = \"inline\"");

Use: document.getElementById("t_"+i).display="";

Vjekoslav
 
L

Lasse Reichstein Nielsen

atomo said:
Hi, i'm wondering why this hiding and showing function works on IE (6) but
it doesn't work in Mozilla Firebird (0.7)

I wonder why it works in IE. But, alas, it does, and we have to live
with it.
function fnc_show(num){

for(i=1;i<7;i++){
eval("t_" + i + ".style.display = \"none\"");

Never use eval to find the value of a variable or property. Never.
Almost never use eval at all. It is slow and inefficient, and it
hides errors far too well.

You have an element with the id "t_1". You then assume that that
element is available as a global variable of the same name. That
is true for IE, but luckily not for many other browsers (no need
to pollute the global namespace like that!).

To get an element from its id, use
document.getElementById("t_"+i).style.display = "none";
}
eval("t_" + num + ".style.display = \"inline\"");

and

document.getElementById("t_"+num).style.display = "inline";
}

t_1, t_2 ...t_7 are the ids for TR elements in a table, i.e. with
fnc_show(3), it will show only t_3. This works without a problem on IE, but
it seems to do nothing in Mozilla, i only get an error telling that t_1 is
not defined.

That is because there is no global variable called "t_1".
I'm wondering too, if there is a better way to do it, cause i read that eval
function is inefficient .

Oh, yes! Very inefficient! And there is (almost) always a better way.
What that better way is, depends on what you try to do.
The FAQ says: <URL:http://jibbering.com/faq/#FAQ4_40>

/L
 
A

atomo

Thanks!!!!
it worked fine.

Lasse Reichstein Nielsen said:
I wonder why it works in IE. But, alas, it does, and we have to live
with it.


Never use eval to find the value of a variable or property. Never.
Almost never use eval at all. It is slow and inefficient, and it
hides errors far too well.

You have an element with the id "t_1". You then assume that that
element is available as a global variable of the same name. That
is true for IE, but luckily not for many other browsers (no need
to pollute the global namespace like that!).

To get an element from its id, use
document.getElementById("t_"+i).style.display = "none";


and

document.getElementById("t_"+num).style.display = "inline";


That is because there is no global variable called "t_1".


Oh, yes! Very inefficient! And there is (almost) always a better way.
What that better way is, depends on what you try to do.
The FAQ says: <URL:http://jibbering.com/faq/#FAQ4_40>

/L
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top