Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Apr 2006
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Script not working in FireFox 2.0

    Hello,
    I have a simple script that worked in FireFox 1, and doesn't work in FireFox 2.
    This is thr code:
    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?

  • #2
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    How are you calling the function? This works fine.
    Code:
    <!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.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #3
    Banned
    Join Date
    May 2006
    Location
    England
    Posts
    664
    Thanks
    0
    Thanked 84 Times in 84 Posts
    Quote Originally Posted by mamachika View Post
    Hello,
    I have a simple script that worked in FireFox 1, and doesn't work in FireFox 2.
    This is thr code:
    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.

  • #4
    New to the CF scene
    Join Date
    Apr 2006
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Now I'm really confused

    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:
    Code:
    <!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:
    Code:
    <!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....

  • #5
    Senior Coder
    Join Date
    Feb 2003
    Posts
    1,665
    Thanks
    0
    Thanked 27 Times in 25 Posts
    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);

  • #6
    New to the CF scene
    Join Date
    Apr 2006
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you so much! changing the case worked


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •