/    Sign up×
Community /Pin to ProfileBookmark

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.

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@DrieseauthorApr 04.2013 — reappears* instead of reapers xD
Copy linkTweet thisAlerts:
@rtretheweyApr 04.2013 — 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!
×

Success!

Help @Driese spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.7,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...