Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-17-2006, 10:25 AM   PM User | #1
BubikolRamios
Senior Coder

 
Join Date: Dec 2005
Location: Slovenia
Posts: 1,876
Thanks: 114
Thanked 76 Times in 76 Posts
BubikolRamios is on a distinguished road
alternate change img src

Suppose I have <IMG src = "A.jpg">
And want to change src alternately on click: A.jpg or B.jpg.

could do this like that:

Code:
var identifer;

function imgOnClick()

{
 if identifer = "A.jpg"
 {
   document.getelementbyid("idOfImgElement").src = "B.jpg":
   identifer = "B.jpg";
 {
 elseif identifer = "B.jpg"
 {
   document.getelementbyid("idOfImgElement").src = "A.jpg":
   identifer = "A.jpg";
 {

}
any sugestions for more clear code(I dont like having "var identifer;") or perhaps even css ?
BubikolRamios is offline   Reply With Quote
Old 12-17-2006, 01:06 PM   PM User | #2
whizard
Senior Coder

 
whizard's Avatar
 
Join Date: Jan 2005
Location: Philadelphia, PA, USA
Posts: 1,457
Thanks: 10
Thanked 37 Times in 37 Posts
whizard will become famous soon enoughwhizard will become famous soon enough
CSS can't do onClick, but it can do

:hover (essentially the same as onMouseOver)

div
{
background-image:a.jpg;
}

div:hover
{
background-image:b.jpg;
}

I don't think IE supports the :hover psuedoclass for anything except links, though

Dan
__________________
If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.
Step 1: Learn. Step 2: Search. Step 3: Post here.
whizard is offline   Reply With Quote
Old 12-17-2006, 01:45 PM   PM User | #3
Mr J
Senior Coder

 
Join Date: Aug 2002
Location: UK
Posts: 2,789
Thanks: 2
Thanked 14 Times in 14 Posts
Mr J is on a distinguished road
Here's one possibility

PHP Code:
function imgOnClick(){

imgEl=document.getElementById("idOfImgElement")

if(
imgEl.src.indexOf("A.jpg")!= -1){
imgEl.src "B.jpg"
}
else{
imgEl.src "A.jpg"
}


__________________
The silent one.

The most dangerous thing in the world is an idea.
The most dangerous person in the world is the one with an idea.
Mr J is offline   Reply With Quote
Old 12-17-2006, 01:49 PM   PM User | #4
Bill Posters
Senior Coder

 
Join Date: Feb 2003
Posts: 1,665
Thanks: 0
Thanked 27 Times in 25 Posts
Bill Posters will become famous soon enough
As you're doing a straight toggle, you don't need to store the other img filename.

Code:
function imgOnClick() {

	var imgEl = document.getElementById('idOfImgElement');
	imgEl.src = (imgEl.src.indexOf('A.jpg') != -1) ? 'B.jpg' : 'A.jpg';

}
Quote:
Originally Posted by whizard View Post


div
{
background-image:a.jpg;
}

div:hover
{
background-image:b.jpg;
}
*ahem*

That would be…
Code:
…
background-image: url(b.jpg);
…
Quote:
I don't think IE supports the :hover psuedoclass for anything except links, though
That's IE6 and below. IE7 now supports it for all elements.

Last edited by Bill Posters; 12-17-2006 at 01:52 PM..
Bill Posters is offline   Reply With Quote
Old 12-17-2006, 02:24 PM   PM User | #5
whizard
Senior Coder

 
whizard's Avatar
 
Join Date: Jan 2005
Location: Philadelphia, PA, USA
Posts: 1,457
Thanks: 10
Thanked 37 Times in 37 Posts
whizard will become famous soon enoughwhizard will become famous soon enough
Quote:
*ahem*
Yeah, sorry, I should have included it... I was thinking that it would be understood, not sure why lol

Dan
__________________
If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.
Step 1: Learn. Step 2: Search. Step 3: Post here.
whizard is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:48 PM.


Advertisement
Log in to turn off these ads.