/    Sign up×
Community /Pin to ProfileBookmark

Javascript that hides a DIV with display:none

Hi! This is my first post here.

My Problem is, that Ive written a Javascript-Funktion that hides a DIV when clicking on an image. But the Script only runs proper on IE but not Opera and Mozilla (other browsers I didnt try).
Maybe anyone can tell my why Opera and Mozilla dont get the script right.
I wrote a similar script a while ago, but instead of working with “parentNode” and “nextSibling” I assigned IDs to the elements. That script did it the right way, also in Opera and Mozilla. But this isnt what Im after since you will have to give every new DIV an individual ID….but maybe you guys just take a look at the code…

[code=html]<html>
<head>
<meta http-equiv=”content-type” content=”text/html;charset=iso-8859-1″>
<meta name=”generator” content=”Adobe GoLive”>
<title>Layout_Testseite</title>
<style type=”text/css” media=”screen”><!–
body { text-align: center; margin: 50px 0 0; padding: 0 }
#container { margin: 0 auto; width: 350px }
.box { border: solid 1px #999 }
–></style>
<script type=”text/javascript”>
function changeVisibility(element){
var handledElement = element.parentNode.nextSibling;
handledElement.style.display = (handledElement.style.display == “none”) ? “” : “none”;
}
</script>
</head>
<body>
<div id=”container”>
<div class=”box”>
<div>
<img src=”” alt=”Auf- und Zuklappen” height=”15″ width=”15″ onclick=”changeVisibility(this)”>
Formular
</div>
<div>
sdaadasdadadada<br>
sdadadada<br>
asdadad<br>
sdasdasd<br>
asdadas<br>
sdadasdadasdasads<br>
adad
</div>
</div>
</div>
</body>
</html>[/code]

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@ccoppenbargerOct 26.2005 — Try this:

<script type="text/javascript">

function changeVisibility(o){

document.getElementById(o).style.display='none';

}

</script>

<body>

<div id="image" style="display:block;">

<img src="Whatever.jpg" alt="Whatever" onclick="changeVisibility('image');" height="15" width="15" />

</div>

Or you can put the id on the img, but you need the style on the img too if you go this right. 'this' would work for the objeect also, but getElementById('') is the cross-browser compatible. You can't change the display of something if it hasn't been set yet, hence the style="display:block;". I hope this helps.
×

Success!

Help @hmpfgr 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,
)...