/    Sign up×
Community /Pin to ProfileBookmark

click image to make it disappear?

Hi,

I am pretty new to JavaScript and want a little help with a problem. Basically I want to make an image disappear when it is clicked to reveal the image behind it. I do not know if this is possible with javascript, so need the advice of some more experienced users.
So say there is a white box in the background, that is 4 times bigger than a smaller black rectangle which cuts across the white box. I want to be able to click on the black rectangle, make it disappear and reveal what is behing it (ie the white box.)

Thanks

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@japaternosterauthorOct 14.2007 — After having a longer search around the internet, I found a solution and have adapted it to my needs. If there is a better way of achieving this, please let me know.

Thanks

[CODE]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<script language="JavaScript">
/*
Hiding image
Author: Narayan Chand Thakur
Source: http://ncthakur.itgo.com/
This may be used freely as long as this message is intact.
*/
<!--

function hideImage(){
if (document.layers){
document.layimage1.visibility="hide"
}
else if(document.all||document.getElementById){
(document.getElementById)?
nct=document.getElementById("docimage")
: nct=document.all.docimage
nct.style.visibility="hidden";
}
}
//-->
</script>
</head>

<body>
<div id="docimage" style="position:absolute" left="280" top="300">
<layer id="layimage1" left="280" top="300">
<p style="cursor:hand"><a href="#" onclick="hideImage();return true"><img src='ball-2.png' border='0'></p>
</body>
</html>
[/CODE]
Copy linkTweet thisAlerts:
@Declan1991Oct 14.2007 — Yes there is. If you have more than one image, do this:
[code=html]
<head>
<script type="text/javascript" src="disappear.js"></script>
</head>
<body>
<img src="" alt="" class="disappear" />
</body>
[/code]

and put the class, disappear, for each img.

Then disappear.jas should have this:
[code=php]
document.getElementsByClassName = function(className,tagName) {
if (!document.getElementsByTagName) {
return false;
}
var elements = document.getElementsByTagName(tagName||"*");
var length = elements.length;
var classElements = new Array();
for (var i = 0; i < length; i++) {
if (elements[i].className.match(className)) {
classElements[classElements.length]=elements[i];
}
}
return (classElements.length>0)?classElements:false;
}
window.addOnloadEvent = function(addToOnload) {
var oldOnload;
if (window.onload) {
oldOnload = window.onload;
window.onload = function(){
oldOnload();
addToOnload();
};
}
else {
window.onload = addToOnload;
}
}
window.addOnloadEvent(doOnclicks);
function doOnclicks() {
var imgs = document.getElementsByClassName("disappear","img");
for (var i = 0; i < imgs.length; i++) {
imgs[i].onclick = function(){this.style.display = "none";};
}
}
[/code]
×

Success!

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