plutomed Posted January 26, 2009 Share Posted January 26, 2009 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. Quote Link to comment Share on other sites More sharing options...
rhodesa Posted January 26, 2009 Share Posted January 26, 2009 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"; } Quote Link to comment Share on other sites More sharing options...
plutomed Posted January 26, 2009 Author Share Posted January 26, 2009 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. Quote Link to comment Share on other sites More sharing options...
rhodesa Posted January 26, 2009 Share Posted January 26, 2009 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); } } Quote Link to comment Share on other sites More sharing options...
plutomed Posted January 26, 2009 Author Share Posted January 26, 2009 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? Quote Link to comment Share on other sites More sharing options...
rhodesa Posted January 26, 2009 Share Posted January 26, 2009 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 = "" } Quote Link to comment Share on other sites More sharing options...
plutomed Posted January 26, 2009 Author Share Posted January 26, 2009 That works. Thanx alot. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.