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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sunny1009
    New Member
    • Mar 2009
    • 5

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

    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
    Code:
    function toggleDiv(id,flagit) {
    if (flagit=="1"){
    if (document.layers) document.layers[''+id+''].visibility = "show"
    else if (document.all) document.all[''+id+''].style.visibility = "visible"
    else if (document.getElementById) document.getElementById(''+id+'').style.visibility = "visible"
    }
    
    else
    if (flagit=="0"){
    if (document.layers) document.layers[''+id+''].visibility = "hide"
    else if (document.all) document.all[''+id+''].style.visibility = "hidden"
    else if (document.getElementById) document.getElementById(''+id+'').style.visibility = "hidden"
    }
    }
  • sunny1009
    New Member
    • Mar 2009
    • 5

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

    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

    Code:
    function toggleDiv(id,flagit) {
    if (flagit=="1"){
    if (document.layers) document.layers[''+id+''].visibility = "show"
    else if (document.all) document.all[''+id+''].style.visibility = "visible"
    else if (document.getElementById) document.getElementById(''+id+'').style.visibility = "visible"
    }
    else
    if (flagit=="0"){
    if (document.layers) document.layers[''+id+''].visibility = "hide"
    else if (document.all) document.all[''+id+''].style.visibility = "hidden"
    else if (document.getElementById) document.getElementById(''+id+'').style.visibility = "hidden"
    }
    }

    Comment

    • sunny1009
      New Member
      • Mar 2009
      • 5

      #3
      I am waiting guys. Please help me..........

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        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)

        Comment

        • sunny1009
          New Member
          • Mar 2009
          • 5

          #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.getEle mentById but I am not sure how can I edit this script so that I can use document.getEle mentById instead of document.layers or document.all. I would really appreciate if you could please update my script.

          Thanks

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            a more modern approach
            Code:
            function toggleDiv(id,flagit) 
            {
              var vis, elem;
            
              if (1 == flagit)
              {
                vis = "visible";
              }
              else if (0 == flagit)
              {
                vis = "hidden";
              }
              else
              {
                throw new RangeError("Unknown Flag");
              }
            
              if (elem = document.getElementById(id))
              {
                elem.style.visibility = vis;
              }
              else
              {
                throw new TypeError("Element with id '"+id+"' does not exist.");
              }
              return vis;
            }
            if you want a function to toggle the visibility, just read the style.visibilit y property and change it to the opposite value (no need to pass a flag)

            Comment

            • sunny1009
              New Member
              • Mar 2009
              • 5

              #7
              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

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                put the following code around your function

                Code:
                try 
                {
                    toggleDiv(…); // with your values
                }
                catch (e)
                {
                    alert(e.name+": "+e.message);
                }

                Comment

                Working...