Skip to content
WJunction - Webmaster Forum

Jquery help

Status
Not open for further replies.
Does anyone know how to change an attribute with a another attribute on a function?

like:

<img src="me.jpg" somethin="somethin.jpg" />

I'm trying to change "me.jpg" to "somethin.jpg" on mouse over but I can't seem to figure it out. I'm just a newb at javascript and jquery :facepalm:
 

5 comments

Try this.

Code:
$('img').mouseover(function() {
  $(this).attr('src','something.jpg');
}).mouseout(function(){
    $(this).attr('src','me.jpg');
  });
 
If you want to use it like in your example (adding a second html property and switch between them on mouseover), you can use the following code:
Code:
$(function() { // short version for document.ready()
  $(img).hover(function() {
    var original = $(this).attr('src');
    $(this).attr('src',$(this).attr('somethin')).attr('somethin',original);
  }, function() {
    var alt = $(this).attr('src');
    $(this).attr('src',$(this).attr('somethin')).attr('somethin',alt);
  });
});
 
@himal
thanks man, but that's what I keep on finding on the net, I have many images so I'm not after just replacing the value of the attribute

@deliteblogger
thanks! this looks like just what I need! I'll try it and see if it works. I was only using this.somethin and thought it would just referrence and change it, obviously not lol :D

@soft2050
thanks :) I'm using jfiddle cause it can run different versions of jquery.
 
Status
Not open for further replies.

About the author

M
Active Member · Joined
1,420
Messages
56
Reactions
48
Points

Advertise on WJunction

Reach 1000's of webmasters, hosts & affiliates. Banner & sponsored-thread slots available.

Contact us
Back
Top Bottom