www.webdeveloper.com
Results 1 to 3 of 3

Thread: Javascript Display/hide

Hybrid View

  1. #1
    Join Date
    Apr 2013
    Posts
    2

    Javascript Display/hide

    Hello, i have almost no experience with Javascript but i had a problem which required me to use it.
    the problem is a user should be able to hide/display a certain div which is located on the index and viewable from the entire site.

    I found a very helpful code online but it still has one problem, as soon as a internal link or button is clicked it resets the hidden div to it's default status (for example, a user hides the div, then clicks a other button and then the div just reapers because display is it's current default status).

    The Code:

    <script type="text/javascript">
    function toggle_visibility(id)
    {
    var e = document.getElementById(id);
    e.style.display = ((e.style.display!='none') ? 'none' : 'block');
    }
    </script>

    <span onclick="toggle_visibility('divname');"> hide/display </span>

    <div id="divname" >
    Div Content
    </div>

    I hope somebody has the time & experience to help me.

  2. #2
    Join Date
    Apr 2013
    Posts
    2
    reappears* instead of reapers xD

  3. #3
    Join Date
    Mar 2011
    Posts
    1,139
    When a user clicks on a link or a button, a "new" page is loaded in the browser (even if its the same URL), and the state of the current page is lost unless you take steps to preserve the necessary information. In this case, you'd need to save the value of the 'display' property of the 'divname' <div>, then pass that information to all subsequent pages so they can render "new" pages using that information. This is often done by passing the information in a query string attached to the URL of the new pages (ie. 'http://www.example.com/somepage.html?divname=block'). The new pages retreive that information using JavaScript to check 'location.search' for the presence of a query string, decoding it, and taking whatever steps are required.

    It's simple enough, but may be beyond your current experience if all you do is copy and paste code and its impossible to provide a solution here without seeing all of your code. You might take this as an opportunity to actually learn how to program in JavaScript. There are lots of online tutorials and it doesn't take that long to learn the basics. Good luck!
    Rick Trethewey
    Rainbo Design

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles