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
https://forums.phpfreaks.com/topic/142501-solved-trycatch/
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
https://forums.phpfreaks.com/topic/142501-solved-trycatch/#findComment-746722
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
https://forums.phpfreaks.com/topic/142501-solved-trycatch/#findComment-746727
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
https://forums.phpfreaks.com/topic/142501-solved-trycatch/#findComment-746728
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
https://forums.phpfreaks.com/topic/142501-solved-trycatch/#findComment-746747
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
https://forums.phpfreaks.com/topic/142501-solved-trycatch/#findComment-746756
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.