Results 1 to 6 of 6

Thread: document.getElementById crashing

  1. #1
    Join Date
    Jun 2008
    Posts
    192
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default document.getElementById crashing

    Hello,

    I have a website that I need help with. You can visit it here: http://www.shahspace.com/bow/home.html. To see what I need help with, click on the services menu item and observe the alert message that comes up. It should say "point 1".

    Here is the code that executes that:

    Code:
    function enableScrolling(menuName)
    {
    	alert("point 1");
    	var menu = document.getElemenetById(menuName);
    	alert("point 2");
    ...
    }
    As you can see, there should be a second alert message that says "point 2", but there isn't. The script is crash when I try to assign to menu the object returned by document.getElementById(menuName). I have verified that menuName is correct. So why would this be crashing?

    To get a fuller understanding of my algorithm, I'll give you the following:

    The menu div:
    Code:
    <div id="services_menu" class="services_menu">
    ...
    </div>
    The "services" button:
    Code:
    <td><a href="#" onclick="displayMenu('services'); return false;"><img src="services.jpg" border=0></a></td>
    The displayMenu() function:
    Code:
    function displayMenu(menu)
    {
    	var menuName = menu + "_menu";
    	document.getElementById(menuName).style.display = "block";
    	
    	if (itemCount(menuName) >= 12)
    	{
    		enableScrolling(menuName);
    	}
    	else
    	{
    		disableScrolling(menuName);
    	}
    }
    The itemCount() function:
    Code:
    function itemCount(menuName)
    {
    	var menu = document.getElementById(menuName);
    	var divArray = menu.getElementsByTagName("div");
    	return divArray.length;
    }
    And the enableScrolling() function I already posted above.

    So essentially, when the user clicks on "service", the services menu becomes visible. If the menu contains 12 items or more, scrolling needs to be enabled. This entails setting the menu to a fixed height and allowing the user to scroll through it (scrolling, in this case, will be customized--I'm not using the div's native scrolling feature).

    The really odd thing is that in itemCount(), I have exactly the same line:

    var menu = document.getElementById(menuName);

    and it works fine there. Why not in my enableScrolling() function?

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Typo (extra e in getElementById):

    Code:
    function enableScrolling(menuName)
    {
    	alert("point 1");
    	var menu = document.getElemenetById(menuName);
    	alert("point 2");
    ...
    }
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. The Following User Says Thank You to jscheuer1 For This Useful Post:

    gib65 (03-14-2012)

  4. #3
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default

    Doesn't this code

    Code:
    var menu = document.getElemenetById(menuName);
    try and use getelementbyid on the variable menuName.

    Shouldn't it be

    Code:
    var menu = document.getElemenetById("menuName");
    P.s. I'm pretty sure thats how you spell it John. Why did you bring that up???

  5. #4
    Join Date
    Jun 2008
    Posts
    192
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by jscheuer1 View Post
    Typo (extra e in getElementById):


    It seems it takes a second pair of eyes to catch those mistakes. Even the first pair has scanned it over and over again.

  6. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by keyboard1333 View Post
    Doesn't this code

    Code:
    var menu = document.getElemenetById(menuName);
    try and use getelementbyid on the variable menuName.

    Shouldn't it be

    Code:
    var menu = document.getElemenetById("menuName");
    P.s. I'm pretty sure thats how you spell it John. Why did you bring that up???
    No you don't need to quote it, it's already a variable representing a string.

    And I brought it up - the spelling, because gib65 spelled it incorrectly, with an extra e. It should be:

    getElementById

    Not:

    getElemenetById

    as gib65 has it in the enableScrolling function, and as you copied it to your post where it's also spelled incorrectly in your code blocks.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #6
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default

    Ahhh, that makes more sense.

Bookmarks

Posting Permissions

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