treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Javasript show/hide with password

  • Hi everyone,

    I have a basic java script code to show and hide a div element. What I want to do is to make the element hide only when a password is entered.

    Here's what I am aiming to do: Basically I want to cover my website with a div element 100% width and height, with a message saying the website is in development. And I want to set a pass code which can be entered to hide the div element and be able to use the website.

    Ok, so the code I have is:

    <a href="javascript:hideshow(document.getElementById('adiv'))">Click here</a>

    <script type="text/javascript">
    function hideshow(which){
    if (!document.getElementById)
    return
    if (which.style.display=="block")
    which.style.display="none"
    else
    which.style.display="block"
    }
    </script>

    <div id="adiv" style="font:24px bold; display: block">Now you see me</div>


    From: www.javascriptkit.com/javatutors/dom3.shtml

    I'd be grateful if anyone can help me.
    Thanks
    Aleksander
  • Hmm..

    I'd use PHP with a isset function. Besides, JS could be off in the browser and it wouldn't work.
  • >>Besides, JS could be off in the browser and it wouldn't work. << <br />true, forgot about that.

    I'm not very good in php, how to use the isset function?
  • Also someone looking at your JS could easily find the password
    There's a million and one ways to do this with PHP

    A very simple way would be to use AJAX to post the password to a PHP file and just use an if statement to compare the POST data with the password before returning the details
  • Ok, thanks will try this AJAX and PHP method. If I'm lost or confused I'll come back here...lol