...

View Full Version : Script not working in FireFox 2.0



mamachika
01-31-2007, 05:15 AM
Hello,
I have a simple script that worked in FireFox 1, and doesn't work in FireFox 2.
This is thr code:


function ShowTreeList(elmtid)
{
var target = document.getElementById("sec"+elmtid);
if (target.style.display == "" || target.style.display == "block")
{
target.style.display = "none";
}
else
{
target.style.display = "block";
}
}


When I used "alert" to get target, it returned "null", while in EI it returned [object]

what should I do to have this script work in all browsers?

_Aerospace_Eng_
01-31-2007, 06:18 AM
How are you calling the function? This works fine.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<script type="text/javascript">
function ShowTreeList(elmtid)
{
var target = document.getElementById("sec"+elmtid);
target.style.display = (target.style.display == '' || target.style.display == 'block') ? 'none' : 'block';
}
</script>
</head>
<body>
<a href="#" onclick="ShowTreeList(1);return false">test</a>
<div id="sec1">test</div>
</body>
</html>
BTW I cleaned up the code a bit by using a ternary operator.

Arty Effem
01-31-2007, 06:19 AM
Hello,
I have a simple script that worked in FireFox 1, and doesn't work in FireFox 2.
This is thr code:


function ShowTreeList(elmtid)
{
var target = document.getElementById("sec"+elmtid);
if (target.style.display == "" || target.style.display == "block")
{
target.style.display = "none";
}
else
{
target.style.display = "block";
}
}


When I used "alert" to get target, it returned "null", while in EI it returned [object]

what should I do to have this script work in all browsers?You need to display the associated HTML.

The likely cause is that at the time the function is called, there is no element with the specified ID attribute.

mamachika
01-31-2007, 09:17 AM
I took Aerospace's script, pasted it onto a new file, and it works fine. Then I altered y original code to match it, and it still doesn't work. I even used a comperison software to see if there's any difference I'm missing, but no!

Aerospace's script, that works:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=windows-1255">
<title>הלול - יום הולדת</title>
<script type="text/javascript">
function ShowTreeList(elmtid)
{
var target = document.getElementById("sec"+elmtid);
alert(target)
target.style.display = (target.style.display == '' || target.style.display == 'block') ? 'none' : 'block';
}
</script>
</head>
<body>
<a href='#' onclick='ShowTreeList(1);return false'>test3</a>
<div id='sec1' style='display:none'>
test...
</div>
</body>
</html>


My code, that doesn't work:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=windows-1255">
<title>הלול - יום הולדת</title>
<script type="text/javascript">
function ShowTreeList(elmtid)
{
var target = document.getElementById("sec"+elmtid);
alert(target)
target.style.display = (target.style.display == '' || target.style.display == 'block') ? 'none' : 'block';
}
</script>
</head>
<body>
<a href='#' onclick='ShowTreeList(1);return false'>test2</a>
<div id='Sec1' style='display:none'>
test...
</div>
</body>
</html>


BTW - the reason I use ' unstead of " is that it's all part of an ASP generated code. but it doesn't seem to matter in Aerospace's script, it still works fine.

I really appreciate your help guys....

Bill Posters
01-31-2007, 09:34 AM
id values are case-sensitive.

The script can't find an element with id="sec1" because none exists. The correct id is Sec1.

so…
var target = document.getElementById("Sec"+elmtid);

mamachika
01-31-2007, 02:46 PM
Thank you so much! changing the case worked :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum