I apparently don't get the syntax of document.getElementById(nameOfDiv).style.visibility='visible';

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • lawrence

    I apparently don't get the syntax of document.getElementById(nameOfDiv).style.visibility='visible';

    The following function correctly makes everything invisible but then
    fails to turn the one chosen DIV back to visible. I imagine I'm
    getting the syntax of the variable wrong? I've tried this with both
    single quotes and no single quotes, but it doesn't work either way.
    What am I doing wrong?


    <SCRIPT type='text/javascript'>
    function makeVisible(nam eOfDiv) {
    document.getEle mentById('weblo gs').style.visi bility='hidden' ;
    document.getEle mentById('entri es').style.visi bility='hidden' ;
    document.getEle mentById('newsl etters').style. visibility='hid den';
    document.getEle mentById('image s').style.visib ility='hidden';
    document.getEle mentById('files ').style.visibi lity='hidden';
    document.getEle mentById('comme nts').style.vis ibility='hidden ';
    document.getEle mentById('links ').style.visibi lity='hidden';
    document.getEle mentById('membe rs').style.visi bility='hidden' ;
    document.getEle mentById('desig n').style.visib ility='hidden';
    document.getEle mentById('setti ngs').style.vis ibility='hidden ';
    document.getEle mentById('categ ories').style.v isibility='hidd en';
    document.getEle mentById('other ').style.visibi lity='hidden';
    document.getEle mentById('onePa nel').style.vis ibility='hidden ';

    document.getEle mentById(nameOf Div).style.visi bility='visible ';
    }
    </script>




    <div id="options">
    <a href="#"><img src="weblogs.gi f"
    onClick="makeVi sible('weblogs' )"></a>
    <a href="#"><img src="weblogEntr ies.gif"
    onClick="makeVi sible('entries' )"></a>
    <a href="#"><img src="webNewslet terPages.gif"
    onClick="makeVi sible('newslett ers')"></a>
    <a href="#"><img src="images.gif "
    onClick="makeVi sible('images') "></a>
    <a href="#"><img src="files.gif" onClick="makeVi sible('files')" ></a>
    <a href="#"><img src="comments.g if"
    onClick="makeVi sible('comments ')"></a>
    <a href="#"><img src="links.gif" onClick="makeVi sible('links')" ></a>
    <a href="#"><img src="categories .gif"
    onClick="makeVi sible('categori es')"></a>
    <a href="#"><img src="users.gif"
    onClick="makeVi sible('members' )"></a>
    <a href="#"><img src="design.gif "
    onClick="makeVi sible('design') "></a>
    <a href="#"><img src="settings.g if"
    onClick="makeVi sible('settings ')"></a>
    <a href="#"><img src="other.gif" onClick="makeVi sible('other')" ></a>
    </div>
  • kaeli

    #2
    Re: I apparently don't get the syntax of document.getEle mentById(nameOf Div).style.visi bility='visible ';

    In article <da7e68e8.04092 11040.5ee43006@ posting.google. com>,
    lkrubner@geocit ies.com enlightened us with...[color=blue]
    > The following function correctly makes everything invisible but then
    > fails to turn the one chosen DIV back to visible. I imagine I'm
    > getting the syntax of the variable wrong? I've tried this with both
    > single quotes and no single quotes, but it doesn't work either way.
    > What am I doing wrong?
    >[/color]

    You have a syntax error somewhere else in code you didn't post.

    Because this works just fine.

    <html>
    <head>
    <title> New Document </title>
    <SCRIPT type='text/javascript'>
    function makeVisible(nam eOfDiv) {
    document.getEle mentById('weblo gs').style.visi bility='hidden' ;
    document.getEle mentById('entri es').style.visi bility='hidden' ;

    document.getEle mentById(nameOf Div).style.visi bility='visible ';
    }
    </script>
    </head>

    <body>
    <div id="weblogs">we blogs</div>
    <div id="entries">en tries</div>
    <div id="options">
    <a href="#"><img src="tile.jpg"
    onClick="makeVi sible('weblogs' )"></a>
    <a href="#"><img src="tile.jpg"
    onClick="makeVi sible('entries' )"></a>
    <p>&nbsp; </p>
    </body>
    </html>

    --
    --
    ~kaeli~
    Black holes were created when God divided by 0.



    Comment

    • lawrence

      #3
      Re: I apparently don't get the syntax of document.getEle mentById(nameOf Div).style.visi bility='visible ';

      kaeli <tiny_one@NOSPA M.comcast.net> wrote in message news:<MPG.1bba4 bd8539cfd9798a1 4c@nntp.lucent. com>...[color=blue]
      > You have a syntax error somewhere else in code you didn't post.
      >
      > Because this works just fine.
      >
      > <html>
      > <head>
      > <title> New Document </title>
      > <SCRIPT type='text/javascript'>
      > function makeVisible(nam eOfDiv) {
      > document.getEle mentById('weblo gs').style.visi bility='hidden' ;
      > document.getEle mentById('entri es').style.visi bility='hidden' ;
      >
      > document.getEle mentById(nameOf Div).style.visi bility='visible ';
      > }
      > </script>
      > </head>
      >
      > <body>
      > <div id="weblogs">we blogs</div>
      > <div id="entries">en tries</div>
      > <div id="options">
      > <a href="#"><img src="tile.jpg"
      > onClick="makeVi sible('weblogs' )"></a>
      > <a href="#"><img src="tile.jpg"
      > onClick="makeVi sible('entries' )"></a>
      > <p>&nbsp; </p>
      > </body>
      > </html>[/color]


      Would it cause a syntax error if I reference a DIV that doesn't exist?
      The code is dying on this line:


      document.getEle mentById('onePa nel').style.vis ibility='hidden ';


      This Javascript is included on every page, and on some pages there is
      no element named "onePanel". Could that cause problems? How would I
      test for "onePanel"?

      Comment

      • Lee

        #4
        Re: I apparently don't get the syntax of document.getEle mentById(nameOf Div).style.visi bility='visible ';

        lawrence said:
        [color=blue]
        >Would it cause a syntax error if I reference a DIV that doesn't exist?
        >The code is dying on this line:
        >
        >
        > document.getEle mentById('onePa nel').style.vis ibility='hidden ';
        >
        >
        >This Javascript is included on every page, and on some pages there is
        >no element named "onePanel". Could that cause problems? How would I
        >test for "onePanel"?[/color]

        That wouldn't be a syntax error, but it would certainly be a run-time
        error.


        if(document.get ElementById('on ePanel')){
        document.getEle mentById('onePa nel').style.vis ibility='hidden ';
        }

        Comment

        • kaeli

          #5
          Re: I apparently don't get the syntax of document.getEle mentById(nameOf Div).style.visi bility='visible ';

          In article <da7e68e8.04092 20949.556683b2@ posting.google. com>,
          lkrubner@geocit ies.com enlightened us with...

          [color=blue]
          > Would it cause a syntax error if I reference a DIV that doesn't exist?[/color]

          No, but it will be a runtime error that will stop processing.
          [color=blue]
          > The code is dying on this line:
          >
          >
          > document.getEle mentById('onePa nel').style.vis ibility='hidden ';
          >
          >
          > This Javascript is included on every page, and on some pages there is
          > no element named "onePanel". Could that cause problems? How would I
          > test for "onePanel"?
          >[/color]

          If objects may or may not exist, test for them. All of them.

          You should also be checking if the browser even supports getElementById if
          this is on the 'net. Also, not all browsers that support that also support
          style. Older versions of Opera had problems with it, IIRC, and there are
          countless browsers out there now that may or may not support it.

          <SCRIPT type='text/javascript'>
          function makeVisible(nam eOfDiv) {
          if (document.getEl ementById) {
          if (document.getEl ementById('webl ogs') &&
          document.getEle mentById('weblo gs').style) {
          document.getEle mentById('weblo gs').style.visi bility='hidden' ;
          }
          // and so on
          }
          }
          </script>



          --
          --
          ~kaeli~
          He often broke into song because he couldn't find the key.



          Comment

          • Michael Winter

            #6
            Re: I apparently don't get the syntax of document.getEle mentById(nameOf Div).style.visi bility='visible ';

            On Wed, 22 Sep 2004 15:00:20 -0500, kaeli <tiny_one@NOSPA M.comcast.net>
            wrote:

            [snip]
            [color=blue]
            > function makeVisible(nam eOfDiv) {
            > if (document.getEl ementById) {
            > if (document.getEl ementById('webl ogs') &&
            > document.getEle mentById('weblo gs').style) {
            > document.getEle mentById('weblo gs').style.visi bility='hidden' ;
            > }
            > // and so on
            > }
            > }[/color]

            Or

            /* Ensure that trying to obtain a reference is always meaningful.
            * Note: That's not the same as always getting a reference.
            */
            var getRefById = document.getEle mentById ?
            function(n) {return document.getEle mentById(n);}
            : document.all ?
            function(n) {
            /* Ensure that the document.all collection
            * returns only one element and that it
            * matches by id, not name.
            */
            var e = document.all[n], l = e.length;
            if('number' == typeof l) {
            for(var i = 0; i < l; ++i) {
            if(e[i].id == n) {return e[i];}
            }
            } else if(e.id == n) {return e;}
            return null;
            }
            : function() {return null;};

            function setVisibility(o , v) {
            if(o) {(o.style || o).visibility = v;}
            }

            function makeVisible(id) {
            setVisibility(g etRefById('webl ogs'), 'hidden');
            // ...
            setVisibility(g etRefById(id), 'visible');
            }

            Whilst it may look less efficient at first glance, the getRefById
            assignment is a one-off cost. After that, getRefById and setVisibility
            encapsulate the testing of properties and the change of style.

            If you don't like that, at least save the return from the getElementById
            call(s). Repeated look-ups are slower and produce larger code.

            Mike

            --
            Michael Winter
            Replace ".invalid" with ".uk" to reply by e-mail.

            Comment

            • lawrence

              #7
              Re: I apparently don't get the syntax of document.getEle mentById(nameOf Div).style.visi bility='visible ';

              kaeli <tiny_one@NOSPA M.comcast.net> wrote in message news:<MPG.1bbb9 5cb24dc888198a1 58@nntp.lucent. com>...[color=blue]
              > In article <da7e68e8.04092 20949.556683b2@ posting.google. com>,
              > lkrubner@geocit ies.com enlightened us with...
              >
              >[color=green]
              > > Would it cause a syntax error if I reference a DIV that doesn't exist?[/color]
              >
              > No, but it will be a runtime error that will stop processing.[/color]

              That's must be what's happening then.



              [color=blue]
              > If objects may or may not exist, test for them. All of them.
              >
              > You should also be checking if the browser even supports getElementById if
              > this is on the 'net. Also, not all browsers that support that also support
              > style. Older versions of Opera had problems with it, IIRC, and there are
              > countless browsers out there now that may or may not support it.
              >
              > <SCRIPT type='text/javascript'>
              > function makeVisible(nam eOfDiv) {
              > if (document.getEl ementById) {
              > if (document.getEl ementById('webl ogs') &&
              > document.getEle mentById('weblo gs').style) {
              > document.getEle mentById('weblo gs').style.visi bility='hidden' ;
              > }
              > // and so on
              > }
              > }
              > </script>[/color]

              This is hot stuff. I wasn't sure how to test for things in Javascript
              but it looks pretty simple. Using the dot notation, Javascript
              apparently resolves the expression and sees if the result is true?
              That's common enough in all languages, I guess, though I'm still
              getting used to how flexible Javascript's dot notation is. Your line:

              if (document.getEl ementById)

              you're simply checking to see if document has the method
              getElementById? That is awfully easy. In PHP I'd have to use
              method_exists($ document, "getElementById "); I was wondering how to
              do the same in Javascript.

              Comment

              • Michael Winter

                #8
                Re: I apparently don't get the syntax of document.getEle mentById(nameOf Div).style.visi bility='visible ';

                On 22 Sep 2004 20:32:41 -0700, lawrence <lkrubner@geoci ties.com> wrote:

                [snip]
                [color=blue]
                > Your [kaeli's] line:
                >
                > if (document.getEl ementById)
                >
                > you're simply checking to see if document has the method
                > getElementById? That is awfully easy. In PHP I'd have to use
                > method_exists($ document, "getElementById "); I was wondering how to
                > do the same in Javascript.[/color]

                That's what's termed "feature detection". Take a look at section 4.26 of
                the FAQ (and its links):

                <URL:http://jibbering.com/faq/>

                Mike

                --
                Michael Winter
                Replace ".invalid" with ".uk" to reply by e-mail.

                Comment

                • kaeli

                  #9
                  Re: I apparently don't get the syntax of document.getEle mentById(nameOf Div).style.visi bility='visible ';

                  In article <da7e68e8.04092 21932.7a3c722a@ posting.google. com>,
                  lkrubner@geocit ies.com enlightened us with...[color=blue]
                  >
                  > This is hot stuff. I wasn't sure how to test for things in Javascript
                  > but it looks pretty simple.[/color]

                  It can be.
                  [color=blue]
                  > Using the dot notation, Javascript
                  > apparently resolves the expression and sees if the result is true?[/color]

                  Well...not exactly.
                  It has something to do with how expressions are evaluated in JS for testing
                  in if/while/for statements. I can't explain it very well, but basically, I'm
                  testing for an object, and the object doesn't exist, so it's not defined (or
                  null or something) which is comparable to false. JS does the conversion from
                  what it really is to the equivalent of false. (or true, as the case may be)
                  Someone else explained it WAY better (and probably more accurate) than that
                  some time ago on this list.
                  [color=blue]
                  > That's common enough in all languages, I guess, though I'm still
                  > getting used to how flexible Javascript's dot notation is. Your line:
                  >
                  > if (document.getEl ementById)
                  >
                  > you're simply checking to see if document has the method
                  > getElementById?[/color]

                  Close enough.
                  I know it's actually a little more complicated than that, but I suck at
                  explaining things. I'm good at applying things, but I am really bad at
                  explaining why they work. *LOL*

                  This has a blurb.


                  If you'd like more explained about it, post a new question and maybe one of
                  the more eloquent gurus will see it. :p

                  --
                  --
                  ~kaeli~
                  Synonym: the word you use in place of a word you can't
                  spell.



                  Comment

                  • Robert

                    #10
                    Re: I apparently don't get the syntax of document.getEle mentById(nameOf Div).style.visi bility='visible ';

                    In article <da7e68e8.04092 21932.7a3c722a@ posting.google. com>,
                    lkrubner@geocit ies.com (lawrence) wrote:
                    [color=blue]
                    > if (document.getEl ementById)
                    >
                    > you're simply checking to see if document has the method
                    > getElementById?[/color]

                    Everything is a variable in Javascript. Yes, this includes functions.

                    At least this is how I currently think of things.

                    Robert

                    Comment

                    • Lasse Reichstein Nielsen

                      #11
                      Re: I apparently don't get the syntax ofdocument.getE lementById(name OfDiv).style.vi sibility='visib le';

                      Robert <rccharles@my-deja.com> writes:
                      [color=blue]
                      > Everything is a variable in Javascript. Yes, this includes functions.[/color]

                      Actually, there are two concepts that "everything " is one of:
                      variable or property.

                      For "document.getEl ementById", the (global) variable "document" is
                      resolved to an object, and the property "getElementById " of it is
                      read. If it exists, it is most likely a function. Properties that
                      are functions are also called "methods", but that just means
                      "property that is function".

                      Local variables (declared with "var" inside a function body or declared
                      as parameters of functions) are not properties of any accessible object.
                      Properties of objects are not variables [1].

                      /L
                      [1] Except that *global* variables are also properties of the global
                      object, and can be accessed as both.
                      --
                      Lasse Reichstein Nielsen - lrn@hotpop.com
                      DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
                      'Faith without judgement merely degrades the spirit divine.'

                      Comment

                      • Thomas 'PointedEars' Lahn

                        #12
                        Re: I apparently don't get the syntax of document.getEle mentById(nameOf Div).style.visi bility='visible ';

                        lawrence wrote:
                        [color=blue]
                        > kaeli <tiny_one@NOSPA M.comcast.net> wrote in message
                        > news:<MPG.1bbb9 5cb24dc888198a1 58@nntp.lucent. com>...[/color]

                        Please do not post attribution novels.
                        [color=blue][color=green]
                        >> <SCRIPT type='text/javascript'>
                        >> function makeVisible(nam eOfDiv) {
                        >> if (document.getEl ementById) {
                        >> if (document.getEl ementById('webl ogs') &&
                        >> document.getEle mentById('weblo gs').style) {
                        >> document.getEle mentById('weblo gs').style.visi bility='hidden' ;
                        >> }
                        >> // and so on
                        >> }
                        >> }
                        >> </script>[/color]
                        >
                        > This is hot stuff. I wasn't sure how to test for things in Javascript
                        > but it looks pretty simple. Using the dot notation, Javascript
                        > apparently resolves the expression and sees if the result is true?[/color]

                        No, the expression, no matter if the property is referenced using the dot
                        notation or the bracket notation, is resolved to an object reference or a
                        value. If the reference is valid or the value is a true-value, it is
                        converted to `true,' otherwise to `false'. That is why the above function
                        can and should be modified:

                        function makeVisible(nam eOfDiv)
                        {
                        var t;
                        if ((t = typeof document.getEle mentById) == "function"
                        || (t == "object" && document.getEle mentById)) // [1]
                        {
                        var o = document.getEle mentById(nameOf Div); // [2]
                        if (o // [3]
                        && typeof (o = o.style) != "undefined" // [4]
                        && typeof o.visibility != "undefined" ) // [5]
                        {
                        o.visibility = "hidden";
                        }
                        // and so on
                        }
                        }

                        [1] Testing that the call operator is applicable to the property,
                        i.e. it is really a method, is better than only testing if
                        the property exists and has a value that converts to `true'.
                        However, if compatibility to JavaScript 1.0 script engine
                        is required, the "typeof" operator cannot be used.

                        [2] The argument of the function should be used here.

                        [3] This will prevent the script from yielding runtime errors if
                        there is either no such object or if the result of getElementById( )
                        is undefined (see W3C DOM Level 1+ HTML).

                        [4] Testing for the value of the "style" property alone may not result in
                        desired behavior; there are DOMs which return the value of the "style"
                        attribute if the style object is read-accessed directly; if the "style"
                        attribute has not been set, the function will not change the
                        "visibility " property even if that would be possible.

                        [5] When writing to values of properties of host objects, it should be
                        tested whether these properties already exist prior; if they do not,
                        ECMAScript Ed. 3 allows a compliant implementation to exhibit any
                        behavior in the case the property must be added first.
                        [color=blue]
                        > That's common enough in all languages, I guess, though I'm still
                        > getting used to how flexible Javascript's dot notation is. Your line:
                        >
                        > if (document.getEl ementById)
                        >
                        > you're simply checking to see if document has the method
                        > getElementById?[/color]

                        No, he is only checking if the object refered to by "document" has
                        a _property_ named "getElementById ", he does not check if it is a
                        function-property, a method; therefore modification [1] above.


                        PointedEars
                        --
                        Troubled words of a troubled mind

                        Comment

                        • Dr John Stockton

                          #13
                          Re: I apparently don't get the syntax of document.getEle mentById(nameOf Div).style.visi bility='visible ';

                          JRS: In article <1101273.SkYnni E2LL@PointedEar s.de>, dated Sun, 3 Oct
                          2004 18:22:21, seen in news:comp.lang. javascript, Thomas 'PointedEars'
                          Lahn <PointedEars@we b.de> posted :[color=blue]
                          >lawrence wrote:
                          >[color=green]
                          >> kaeli <tiny_one@NOSPA M.comcast.net> wrote in message
                          >> news:<MPG.1bbb9 5cb24dc888198a1 58@nntp.lucent. com>...[/color]
                          >
                          >Please do not post attribution novels.[/color]


                          Present Usenet thinking encourages proper attributions; see Usefor work-
                          in-progress at



                          Once more, you are reviving an elderly thread without giving visible
                          warning to users of standards-compliant newsreaders; that is
                          discourteous, and mot at all like the typical modern German.

                          Someone please quote this, or use it when next appropriate; Thomas Lahn
                          appears to prefer not to read anything that might criticise him.

                          His signature is also not in compliance with good Usenet practice.

                          --
                          © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 MIME ©
                          Web <URL:http://www.uwasa.fi/~ts/http/tsfaq.html> -> Timo Salmi: Usenet Q&A.
                          Web <URL:http://www.merlyn.demo n.co.uk/news-use.htm> : about usage of News.
                          No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.

                          Comment

                          Working...