Announcement

Collapse
No announcement yet.

Javascript help

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Javascript help

    I'm try to make a tiny script that will according to the contents of a variable hide/show a layer. But I can't see to get it working I dont get any error or anything any advice.

    <html>

    <head>

    <title>YO</title>

    <style type="text/css">
    .testdiv {
    position: absolute;
    top: 500px;
    let: 100px;
    background-color: #ffcc00;
    width: 200px;
    height: 75px;
    visibility: visible;
    }
    </style>

    <body>
    <div style="position: absolute; top: 400;">Hi<br>
    <script language="javascript">

    var example = "Yes";
    var layergrabber;
    layergrabber = document.getElementById("testdiv");

    if (example == "Yes") {
    document.layergrabber.style.visibility="hidden";
    {
    else {
    document.write("false");
    }
    </script>
    </div>
    <div class="testdiv"></div>
    </body>
    </html>

    #2
    now you can write what I know about java on a stamp but...

    if (example == "Yes") {
    document.layergrabber.style.visibility="hidden";
    {
    else {
    document.write("false");
    }


    This looks strange, I take it the {'s open and close statements?

    coz here youve not got it tallying up, you open 3 then only close the 1

    just a thought

    Comment


      #3
      Indeed, I can't see initially anything grammatically wrong with the code apart from the lack of a closing } for it. The IF statement should be written as far as I'm aware:

      if (example == "Yes")
      {
      document.layergrabber.style.visibility="hidden";
      }
      else
      {
      document.write("false");
      }
      Lie with passion and be forever damned...

      Comment


        #4
        You shouldn't need braces for an if with no block statement, but yeah he's right theres a missing brace. and one the wrong way

        if (example == "Yes")
        document.layergrabber.style.visibility="hidden";
        else
        document.write("false");



        I guess javascript might need them , but hell I can't remember really.

        Comment


          #5
          done the closing tag, well spotted I've been working on this Menu sript for 3 days and I'm almost finished with it apart from this part of the script, which seems not to work at all. It's certainly something to do with the DOM.... Still it doesn't work after closing that }...

          Comment


            #6
            Originally posted by gingerj

            if (example == "Yes") {
            document.layergrabber.style.visibility="hidden";
            {
            else {
            document.write("false");
            }
            the 2nd brace is the wrong way around, but I doubt this statement needs braces at all.

            Comment


              #7
              I know it's something to do with the DOM statement, as if I swap the if statement with something like document.write("true") replacing the hiding action it works fine. So my DOM line is causing problems.

              for example:

              <script language="javascript">

              var example = "Yes";
              var layergrabber;
              layergrabber = document.getElementById("testdiv");

              if (example == "Yes") {
              document.write("true");
              }
              else {
              document.write("false");
              }
              </script>
              works fine...
              Last edited by gingerj; 15-12-2005, 12:48.

              Comment


                #8
                You don't have anything with an ID of "testdiv" for your document.getElementById to reference. I.e. you have given your div a class definition, but not a name or id.
                So, your div at the bottom should be:
                <div class="testdiv" id="testdiv" name="testdiv"></div>

                you should also reference "layergrabber" in your script directly rather than "document.layergrabber"

                I'm still not convinced that your code will work because your javascript is being called before you have defined your div. You may be better off putting it in a function and calling it when your page loads, something like:

                <html>
                <head>
                <title>YO</title>

                <style type="text/css">
                .testdiv {
                position: absolute;
                top: 500px;
                let: 100px;
                background-color: #ffcc00;
                width: 200px;
                height: 75px;
                visibility: visible;
                }
                </style>
                <script language="javascript">
                function HideDiv(bHide) // boolean, should it hide or not?
                {
                var layergrabber;
                layergrabber = document.getElementById("testdiv");

                if (bHide)
                {
                layergrabber.style.visibility="hidden";
                document.getElementById("TopDiv").innerText = "div set to hidden";
                }
                else
                {
                layergrabber.style.visibility="visible";
                document.getElementById("TopDiv").innerText = "div set to visible";
                }
                }
                </script>
                </head>
                <body onload="HideDiv(true)">
                <div style="position: absolute; top: 400;" id="TopDiv" name="TopDiv">
                </div>
                <input type="button" id="cmdShow" value="Show" onClick="HideDiv(false)">
                <input type="button" id="cmdHide" value="Hide" onClick="HideDiv(true)">
                <div class="testdiv" id="testdiv" name="testdiv"></div>
                </body>
                </html>
                Last edited by davesol; 15-12-2005, 13:11.

                Comment


                  #9
                  You know that bit at the start of Scanners?
                  That's what happens to me whenever I read threads like this.
                  'testdiv' indeed
                  Scanners is on terrestrial telly tomorrow night btw. 1150pm BBC1.

                  Comment

                  Working...
                  X