...

View Full Version : How can I loop images object ?



Nuttasit
10-20-2008, 01:08 PM
Hi,

I would like to know,can I loop images by getElementById() method ?

Here is my code.It cant execute.

function SwitchMenu(obj){
for(i=0; i<obj ; i++ )
{
var img = document.getElementById('bullet'+(i));
img.src = "images/plus.jpg"
}

Thank you so much.

-------------------------------------------------------------------
Actually, I try to do the switch menu.
Here is all code.

Javascript :

function SwitchMenu(obj){
var el = document.getElementById(obj);
var ar = document.getElementsByTagName("span");
var img = document.getElementById('bullet'+(obj));
if(el.style.display != "block"){
for (var i=0; i<ar.length; i++){
if (ar[i].className=="submenu")
ar[i].style.display = "none";
img.src="images/extend.jpg";
}
el.style.display = "block";
}else{
el.style.display = "none";
img.src="images/normal.jpg";
}
}

Html :

<div onclick="SwitchMenu('sub1')" class="toggle" ><img src="images/normal.jpg" border="0" name="bullet1" id="bulletsub1" />&nbsp;CARD &amp; TABLE GAMES</div>
<span class="submenu" id="sub1">
<a href="gameA01_en.asp"> gameA01</a>
<a href="gameA02_en.asp">gameA02</a><br />
<a href="gameA03_en.asp">gameA03</a><br />
<a href="gameA04_en.asp">gameA04</a><br />
</span>

<div onclick="SwitchMenu('sub2')" class="toggle" ><img src="images/normal.jpg" border="0" name="bullet1" id="bulletsub2" />&nbsp;CARD &amp; TABLE GAMES</div>
<span class="submenu" id="sub1">
<a href="gameB01_en.asp"> gameB01</a>
<a href="gameB02_en.asp">gameB02</a><br />
<a href="gameB03_en.asp">gameB03</a><br />
<a href="gameB04_en.asp">gameB04</a><br />
</span>

<div onclick="SwitchMenu('sub3')" class="toggle" ><img src="images/normal.jpg" border="0" name="bullet1" id="bulletsub3" />&nbsp;CARD &amp; TABLE GAMES</div>
<span class="submenu" id="sub1">
<a href="gameC01_en.asp"> gameC01</a>
<a href="gameC02_en.asp">gameC02</a><br />
<a href="gameC03_en.asp">gameC03</a><br />
<a href="gameC04_en.asp">gameC04</a><br />
</span>

Everything is fine but the swap images got a problem. When I clicked another link the image isn't swap.
I have to clicked itself to let it swap. Which way can I fix ?

Thank you for your help.:thumbsup:

Kor
10-20-2008, 03:45 PM
Insufficient data. Can we see the HTML as well? What do you mean by "loop images"?

rnd me
10-21-2008, 05:30 AM
you are missing "}{":



function SwitchMenu(obj){
var el = document.getElementById(obj);
var ar = document.getElementsByTagName("span");
var img = document.getElementById('bullet'+(obj));
if(el.style.display != "block"){
for (var i=0; i<ar.length; i++){
if (ar[i].className=="submenu"){
ar[i].style.display = "none";
img.src="images/extend.jpg";
el.style.display = "block";
}else{
el.style.display = "none";
img.src="images/normal.jpg";
}//end if
}//next

}//end function



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum