Free Advertising - Shops - Discount Perfume - Submit articles - Debt Consolidation

PDA

View Full Version : images don't switch


gilgalbiblewheel
Aug 14th 2007, 10:47 am
Can someone tell me where I've gone wrong in the following code?

I get:
Error: imgElement has no properties
Line: 137

<script type="text/javascript" language="javascript1.3">
function toggle(id){
ol = "ol_" + id;
img = "img_" + id;
olElement = document.getElementById(ol);
imgElement = document.getElementById(img);
if (olElement){
if (olElement.className == 'closed'){
olElement.className = "open";
imgElement.src = "images/opened.gif";
}else{
olElement.className = "closed";
imgElement.src = "images/closed.gif";
}
}
}
</script>

<td>
<a onclick="toggle('item6');">
<h5><img src="images/closed.gif" alt="Click to Expand" id="img_item1" border="0" /> TOP 4 SELF-HELP BOOKS</h5>
</a>
<ol type="1" id="ol_item6" class="closed">
<li id="item6_1"><a href="">Feeling Good</a></li>
<li id="item6_2"><a href="">Resilience</a></li>
<li id="item6_3"><a href="">Allies in Healing</a></li>
<li id="item6_4"><a href="">The Dance of Anger</a></li>
</ol>
</td>

gilgalbiblewheel
Aug 14th 2007, 11:11 am
I'll answer my own question: Because I haven't changed the img id to 6 from one.
<a onclick="toggle('item6');">
<h5><img src="images/closed.gif" alt="Click to Expand" id="img_item1" border="0" /> TOP 4 SELF-HELP BOOKS</h5>
</a>
When I changed it, it worked.

Wildhoney
Aug 14th 2007, 3:44 pm
Give yourself a rep!

koldfyre
Aug 15th 2007, 12:48 am
Hmm, your code is pretty nice. I like how you used the class to check whether something was opened or closed. When I first needed something like that, I used variables first and then went to just checking the style of the object directly.
Something like this:

if(this.style.visibility=="visible"){
this.style.visibility="hidden";
}

and then I learned about ternary operators and did something like this:

(this.style.visibility=="visible") ? (this.style.visibility="hidden") : (this.style.visibility="visible");


Ed

gilgalbiblewheel
Aug 15th 2007, 6:26 am
and then I learned about ternary operators and did something like this:

(this.style.visibility=="visible") ? (this.style.visibility="hidden") : (this.style.visibility="visible");


Ed

Is there a tutorial on that?

Give yourself a rep!
Yeah sometimes I feel so braindead in front of the computer that I don't see the problem right away.

Especially the error "has no properties" leaves me scratching my head and looking everywhere saying what's causing this?