/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Show/Hide Div

Hi Everyone,

I have the code below which works ok but I need to try and simplify it if at all possible.

Is there a way to clean up the javascript code so it doesn’t have all the info for other browsers? We are just using IE6.

Also, i’m trying to setup a template so others can easily use it and just plug in info. I like how the content of either div shows in the same spot but I think it would be confusing to have to keep changing the location fo the div box. Any easier way to do this?

<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″ />
<title>Test Show-Hide</title>
<script language=javascript type=’text/javascript’>

function showdiv(pass) {
var divs = document.getElementsByTagName(‘div’);
for(i=0;i<divs.length;i++){
if(divs[i].id.match(pass)){//if they are ‘see’ divs
if (document.getElementById) // DOM3 = IE5, NS6
divs[i].style.visibility=”visible”;// show/hide
else
if (document.layers) // Netscape 4
document.layers[divs[i]].display = ‘visible’;
else // IE 4
document.all.divs[i].visibility = ‘visible’;
} else {
if (document.getElementById)
divs[i].style.visibility=”hidden”;
else
if (document.layers) // Netscape 4
document.divs[i].visibility = ‘hidden’;
else // IE 4
document.all.divs[i].visibility = ‘hidden’;
}
}
}
</script>
<body>
<a href=”javascript:showdiv(‘Div1’)”>Link for Div1</a> <br>
<a href=”javascript:showdiv(‘Div2’)”>Link for Div2</a>
<p></p>
<div id=”Div1″ style=”position: absolute; left:5px; top:165px; visibility: hidden”>
<p>Text for Div1</p>
</div>

<div id=”Div2″ style=”position: absolute; left:5px; top:165px; visibility: hidden”></a>
<p>Text for Div2</p>
</div>

</body>
</html>

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@rnd_meSep 09.2008 — your posted code look like its from about 12 years ago...

use display, not visibility.
[CODE]
<div id="Div1" style="position: absolute; left:5px; top:165px; display: none">



function showdiv(pass) {
var divs = document.getElementsByTagName('div');
for(i=0, mx=divs.length;i<mx;i++){
divs[i].style.display='none';
}
document.getElementById(pass).style.display = 'block';
}

[/CODE]


this should work in all browsers, including IE6.

a little cleaner huh? ?
Copy linkTweet thisAlerts:
@coded-redauthorSep 09.2008 — Thanks!

I have only basic understanding and all the code I have was cut/paste from stuff I could find in forums. I'll give your suggestions a try ?
Copy linkTweet thisAlerts:
@coded-redauthorSep 09.2008 — Works perfect, thank you so much!
Copy linkTweet thisAlerts:
@rnd_meSep 09.2008 — Works perfect, thank you so much![/QUOTE]

hey no problem.

unsolicited advice:

be careful when cut and pasting, there's a lot of javascript "leftovers" floating around gathering hits. try to shop around for the simplest solution.

avoid anything with "document.layers" like the plague.

the language has gotten pretty refined the last few years, so if you see something that looks complicated, it's probably old. to be more accurate, the language is the same but techniques are getting much better.

anyways, glad to be useful.
×

Success!

Help @coded-red 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.16,
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,
)...