By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,282 Members | 1,548 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,282 IT Pros & Developers. It's quick & easy.

document.all[...].style is null or not an object

P: 5
Hi Guys,

Can you please check this code and see whats wrong with it?? I am getting document.all[...].style is null or not an object error.

Any help will be really appreciated.

Thanks

Code
Expand|Select|Wrap|Line Numbers
  1. function toggleDiv(id,flagit) {
  2. if (flagit=="1"){
  3. if (document.layers) document.layers[''+id+''].visibility = "show"
  4. else if (document.all) document.all[''+id+''].style.visibility = "visible"
  5. else if (document.getElementById) document.getElementById(''+id+'').style.visibility = "visible"
  6. }
  7.  
  8. else
  9. if (flagit=="0"){
  10. if (document.layers) document.layers[''+id+''].visibility = "hide"
  11. else if (document.all) document.all[''+id+''].style.visibility = "hidden"
  12. else if (document.getElementById) document.getElementById(''+id+'').style.visibility = "hidden"
  13. }
  14. }
  15.  
Sep 10 '09 #1
Share this Question
Share on Google+
7 Replies


P: 5
Hi Guys,

Can you please have a look at this code and see whats wrong with it?? I am getting document.all[...].style is null or not an object error. I tried really hard but can't figure out where I am going wrong with this code.

Any help will be really appreciated.

Thanks

Expand|Select|Wrap|Line Numbers
  1. function toggleDiv(id,flagit) {
  2. if (flagit=="1"){
  3. if (document.layers) document.layers[''+id+''].visibility = "show"
  4. else if (document.all) document.all[''+id+''].style.visibility = "visible"
  5. else if (document.getElementById) document.getElementById(''+id+'').style.visibility = "visible"
  6. }
  7. else
  8. if (flagit=="0"){
  9. if (document.layers) document.layers[''+id+''].visibility = "hide"
  10. else if (document.all) document.all[''+id+''].style.visibility = "hidden"
  11. else if (document.getElementById) document.getElementById(''+id+'').style.visibility = "hidden"
  12. }
  13. }
Sep 10 '09 #2

P: 5
I am waiting guys. Please help me..........
Sep 10 '09 #3

Dormilich
Expert Mod 5K+
P: 8,639
today there is hardly any need for document.layers or document.all (you only need that for outdated browsers like NN4 or IE4). just use getElementById() (and if you really need that backward compatibility, use getElementById() as first option)
Sep 10 '09 #4

P: 5
Thanks for your reply Dormilich. I understand that nowdays we dont need to use document.layers or document.all and instead we use document.getElementById but I am not sure how can I edit this script so that I can use document.getElementById instead of document.layers or document.all. I would really appreciate if you could please update my script.

Thanks
Sep 10 '09 #5

Dormilich
Expert Mod 5K+
P: 8,639
a more modern approach
Expand|Select|Wrap|Line Numbers
  1. function toggleDiv(id,flagit) 
  2. {
  3.   var vis, elem;
  4.  
  5.   if (1 == flagit)
  6.   {
  7.     vis = "visible";
  8.   }
  9.   else if (0 == flagit)
  10.   {
  11.     vis = "hidden";
  12.   }
  13.   else
  14.   {
  15.     throw new RangeError("Unknown Flag");
  16.   }
  17.  
  18.   if (elem = document.getElementById(id))
  19.   {
  20.     elem.style.visibility = vis;
  21.   }
  22.   else
  23.   {
  24.     throw new TypeError("Element with id '"+id+"' does not exist.");
  25.   }
  26.   return vis;
  27. }
if you want a function to toggle the visibility, just read the style.visibility property and change it to the opposite value (no need to pass a flag)
Sep 10 '09 #6

P: 5
Hi Dormilich

Thanks for the code mate really appreciate. I change my code to match yours but this time i am getting different error.
Exception thrown and not caught do you know anything about this mate. Please help me if you can.

Thanks
Sep 11 '09 #7

Dormilich
Expert Mod 5K+
P: 8,639
put the following code around your function

Expand|Select|Wrap|Line Numbers
  1. try 
  2. {
  3.     toggleDiv(); // with your values
  4. }
  5. catch (e)
  6. {
  7.     alert(e.name+": "+e.message);
  8. }
Sep 11 '09 #8

Post your reply

Sign in to post your reply or Sign up for a free account.