Jump to content

[SOLVED] Try...Catch


plutomed

Recommended Posts

Can you tell me why this doesn't work?

I have submenus going from 1 to 3

 


function SubMenus(MenuId)
{
start = 1
i = 4
end = 0
try
{
	document.getElementById("SubMenu"+i)
}
catch(error)
{
	alert(error.description)
}
document.getElementById(MenuId).style.display = (document.getElementById(MenuId).style.display == "none") ? "" : "none";
}

 

The problem is that I don't get the error back. "SubMenu4" doesn't exist.

Link to comment
Share on other sites

document.getElementById() doesn't throw an error, it just returns undefined. do something like this instead:


function SubMenus(MenuId)
{
  start = 1
  i = 4
  end = 0
  if(!document.getElementById("SubMenu"+i))
  {
    alert("SubMenu"+i+" doesn't exist")
  }
  document.getElementById(MenuId).style.display = (document.getElementById(MenuId).style.display == "none") ? "" : "none";
}

Link to comment
Share on other sites

document.getElementById() doesn't throw an error, it just returns undefined. do something like this instead:


function SubMenus(MenuId)
{
  start = 1
  i = 4
  end = 0
  if(!document.getElementById("SubMenu"+i))
  {
    alert("SubMenu"+i+" doesn't exist")
  }
  document.getElementById(MenuId).style.display = (document.getElementById(MenuId).style.display == "none") ? "" : "none";
}

 

Yea thanx. I was just playing about with it and found that out. ;)

 

Thanx anyway. :D

Link to comment
Share on other sites

or...you could do:

function SubMenus(MenuId)
{
  start = 1
  i = 4
  end = 0
  try
  {
    document.getElementById(MenuId).style.display = (document.getElementById(MenuId).style.display == "none") ? "" : "none";
  }
  catch(error)
  {
    alert(error);
  }
}

Link to comment
Share on other sites

Ok...Was all working. Its not shoing the div now. :/

 

function SubMenus(MenuId)
{

var start = 0
var i = 0
var end = -1
i=start
while(end != i)
{
	i++;
	if(!document.getElementById("SubMenu"+i))
	{
		end = i	
	}

}

i=0

for(start; end; i++)
{
	document.getElementById("SubMenu"+i).style.display = "none"
}

document.getElementById(MenuId).style.display = ""

}

}

 

Can you see an error there?

Link to comment
Share on other sites

it doesn't jump out at me...but this seems like an easier way:

function SubMenus(MenuId)
{
  for(var i = 1;document.getElementById("SubMenu"+i);i++)
  {
    document.getElementById("SubMenu"+i).style.display = "none"
  }
  document.getElementById(MenuId).style.display = ""
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.