Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

toggle styles 1

Status
Not open for further replies.

darryncooke

Technical User
May 6, 2009
308
US
I have a style with an image and i want to swap styles on click back and forth.

its pretty much an image that i want swapped when clicked once and then swapped back when clicked again and back and forth.

Darryn Cooke
| Marketing and Creative Services
 


Kind of Vague, but here's a stab:

Code:
function toggleImg(imgObj){
 var img1="path/to/image1.jpg";
 var img2="path/to/image2.jpg";

 if(imgObj.src==img1){
  imgObj.src=img2;
 }

 else{
  imgObj.src=img1;
 }

}
...



<img src="..." onclick="toggleImg(this);">

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
its just an image

<img src="path" id="class">

when clicked i want to switch class to class1 - or whatever.

The point is that im planning on creating a sprite and class 2 is going to just move the image up to show the second part of the image.

then on reclick i want class1 to switch back to class.

you can see where this will work at - on the left there is a tab to control the content window to hide and show it. I want to restyle that tab control.

Darryn Cooke
| Marketing and Creative Services
 
If you just want to change the classes its the same basic principle except you target the className rather than the class.

Code:
function toggleImg(imgObj){
 
 if(imgObj.className=="class1"){
  imgObj.className="class2";
 }

 else{
  imgObj.className="class1";
 }

}
...



<img src="..." onclick="toggleImg(this);">

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Me said:
except you target the className rather than the class.

That should read:

except you target the className rather than the src.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top