Forum Moderators: open

Message Too Old, No Replies

Image Replacement

REF: Post #:3062926 11:34 am on Aug. 28, 2006

         

Novacaro

4:32 pm on Jan 26, 2009 (gmt 0)

10+ Year Member


I am a "monkey see, monkey do" site designer, cut 'n paste being my salvation. In other words, if it weren't for the generosity of programmers such as on this site, I would be lost in cyberspace. I am including the code birdbrain posted about having "hide and show" code work in browsers other then IE. It works GREAT for my purposes as well, but I have images on my site pages other than the plus.GIF and minus.GIF that are referenced in the code below. The dilemma is: All the OTHER images are "replaced" by the plus and minus gif's when I the click the text within the span id's to turn divisions "on" and "off" -- How can I remedy this:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>show/hide info</title>
<base href="http://mysite.orange.co.uk/azygous/">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
.container {
font-family:verdana,arial,helvetica,sans-serif;
font-size:16px;
margin:4px 0;
}
.container img {
vertical-align:bottom;
}
.container span {
font-weight:bold;
cursor:pointer;
}
.off {
display:none;
}
.on {
display:block;
margin:10px 30px;
text-align:justify;
color:#003;
background-color:#eef;
}
-->
</style>

<script type="text/javascript">
<!--
window.onload=function() {
spn=document.getElementsByTagName('span');
for(c=0;c<spn.length;c++) {
spn[c].onclick=function() {
if(this.id!='') {
stuff(this.id.split('s')[1]);
}
}
}
}
function stuff(n) {

dvs=document.getElementsByTagName('div');
ims=document.getElementsByTagName('img');
info=document.getElementById('div'+n);
pic=document.getElementById('img'+n);

for(c=0;c<dvs.length;c++) {
if((dvs[c].className=='on')&&(dvs[c].id!='div'+n)) {
dvs[c].className='off';
}
}
for(c=0;c<ims.length;c++) {
ims[c].src='images/plus.GIF';
}
if(info.className=='on') {
info.className='off';
pic.src='images/plus.GIF';
}
else {
info.className='on';
pic.src='images/minus.GIF';
}
}

//-->
</script>

</head>
<body>

<div class="container">
<img id="img0" src="images/plus.GIF" alt=""/>
<span id="s0">John Smith</span>
<div id="div0" class="off">

(truncated here)

Shores

5:12 pm on Jan 26, 2009 (gmt 0)

10+ Year Member



I think i don't understand completely what's your problem, but it seems to me that the problem i here:

window.onload=function() {
spn=document.getElementsByTagName('span');

Here you get all the span elements and apply the click event handler; you should instead add a class="myclass" to the spans you need to be clickable and then

window.onload=function() {
spn=document.getElementsByClassName('myclass');

The same exact reasoning can be applied if wht you want is restrict what images get replaced with the plus/minus gifs, you should make sure that you choose only these images before replacing theyr src attribute.

birdbrain

6:20 pm on Jan 26, 2009 (gmt 0)



Hi there Novacaro,

I am sorry about that.
When I first made that script, I had not taken into account other content that would be on the page.
Since then I have revised it to cure the image problem and to also make it degrade gracefully for
those with javascript disabled.
Here is the revised code...


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>show/hide info</title>
<base href="http://www.coothead.co.uk/images/">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<style type="text/css">
.container {
font-family:verdana,arial,helvetica,sans-serif;
font-size:16px;
margin:4px 0;
}
.container img {
vertical-align:bottom;
}
.container span {
font-weight:bold;
cursor:pointer;
}
.off {
display:none;
}
.on {
display:block;
margin:10px 30px;
text-align:justify;
color:#003;
background-color:#eef;
}
</style>

<script type="text/javascript">
if(window.addEventListener){
window.addEventListener('load',init,false);
}
else {
if(window.attachEvent){
window.attachEvent('onload',init);
}
}

function init(){
spn=document.getElementById('wrapper').getElementsByTagName('span');
for(c=0;c<spn.length;c++) {
document.getElementById('div'+c).className='off';
spn[c].onclick=function() {
if(this.id!='') {
stuff(this.id.split('s')[1]);
}
}
}
}
function stuff(n) {

dvs=document.getElementById('wrapper').getElementsByTagName('div');
ims=document.getElementById('wrapper').getElementsByTagName('img');
info=document.getElementById('div'+n);
pic=document.getElementById('img'+n);

for(c=0;c<dvs.length;c++) {
if((dvs[c].className=='on')&&(dvs[c].id!='div'+n)) {
dvs[c].className='off';
}
}
for(c=0;c<ims.length;c++) {
ims[c].src='plus.GIF';
}
if(info.className=='on') {
info.className='off';
pic.src='plus.GIF';
}
else {
info.className='on';
pic.src='minus.GIF';
}
}

</script>

</head>
<body>

<div id="wrapper">
<div class="container">
<span id="s0"><img id="img0" src="plus.GIF" alt=""/>
John Smith</span>
<div id="div0">
Lorem ipsum
</div>
</div>

<div class="container">
<span id="s1"><img id="img1" src="plus.GIF" alt=""/>
Frank Furter</span>
<div id="div1">
Lorem ipsum
</div>
</div>

<div class="container">
<span id="s2"><img id="img2" src="plus.GIF" alt=""/>
Mary Contrary</span>
<div id="div2">
Lorem ipsum
</div>
</div>

<div class="container">
<span id="s3"><img id="img3" src="plus.GIF" alt=""/>
Joe Bloggs</span>
<div id="div3">
Lorem ipsum
</div>
</div>
</div>

</body>
</html>

Hi there Shores,

Note:-
I believe that the function getElementsByClassName() is at, present, unsupported by IE.

birdbrain