Switch img, alt & title onclick

a glass of wine

javascript
function changePicPlus(el, pic1, alt1, pic2, alt2) {
if ((el.src).match("images/"+pic1+".jpg")!=null) {
el.src="images/"+pic2+".jpg";
el.alt=alt2;
el.title=alt2;
}
else {
el.src="images/"+pic1+".jpg";
el.alt=alt1;
el.title=alt1;
}
}
window.onload=function() {
var pic=document.getElementById('theDrink');
pic.onclick=function(){changePicPlus(this,'wine','a glass of wine','beer','some beer');}
}
HTML
<h1>Switch img, alt &amp; title onclick</h1>
<p><img id="theDrink" src="images/wine.jpg" alt="a glass of wine"></p>

Comments

#1
2009-11-23 marianne says :

chua

#2
2009-11-23 marianne says :

I wanted to know codes for a comment form like this one
here thanks

#3
2010-09-26 mel says :

Is there any way to do this with links?

#4
2010-09-27 BonRouge says :

Mel,
What do you mean exactly?
The action to change the image is a click. If you had a link, the click would go follow the link.

Comment form

Please type the word 'chocolate' here:

BB code available :

  • [b]...[/b] : bold
  • [it]...[/it] : italic
  • [q]...[/q] : quote
  • [c]...[/c] : code
  • [url=...]...[/url] : url