views:

116

answers:

3

Excuse my noobiness when it comes to Jquery, but I have some jquery code that does rollovers for me:

$("img.rollover").hover(function () {
    this.src = this.src.replace("_off","_on");
  }, 
  function () {
    this.src = this.src.replace("_on","_off");
});

Essentially just switching the images from image_on.jpg to image_off.jpg on hover on a bunch of images, now on click i would like to set the state of "image_on" permanently but the hover state seems to overwrite it and it doesn't stay "_on", i'm guessing it something to do with binding of events? I also don't seem to be able to do it so if a user clicks on one image it sets it to on, but if they click another image to return the previous image to the "_off" state and set the current one to the "_on" state.

Any help appreciated, ta.

+1  A: 

i would add a "selected" class to the link when it is actually active and filter against that.

$("img.rollover").bind('click',function(){
    $('img.rollover').removeClass('selected');
    $(this).addClass('selected');
}).hover(function () {
    this.src = this.src.replace("_off","_on");
  }, 
  function () {
if(!$(this).hasClass('selected')){
    this.src = this.src.replace("_on","_off");
}
});

Of course, you would style your .selected images using the _on image.

sidenote: Note that the recommended way of implementing rollover images is the css sprite technique: you use an image that contains all the states, use that image as a background image, and adjust the background-position according to :hover, :active states. It's a pure CSS solution that works in all recent browsers.

pixeline
Still need to account for clicking another image and turning the rest off, maybe a class on these images so a selector works?
Nick Craver
exactly: my code uses the "selected" class for that. It first removes it from the images that have it, then adds the class to the clicked one.
pixeline
@pixeline - This doesn't make much sense then, you're swapping the source on the element, but requiring the same the source to be defined in CSS...very confusing, seems like some `:hover, .selected` CSS would be a better approach, and lop off that whole hover function. I agree with the sprite approach, should update to that entirely.
Nick Craver
Yeah i hsould have used the sprite approach, unfrotunatley now the code is engrained so it'll be a tough shout to revert. Also any idea why this code isn't working? Seems to be a problem around if(!$(this).hasClass('selected') but i'm not sure what the issue is?
Dizzler
@Dizzler - It's missing a paren: `if(!$(this).hasClass('selected')`, needs another `)` on the end there to close.
Nick Craver
oh i see now, mismatched brackets: if(!$(this).hasClass('selected'){ --> if(!$(this).hasClass('selected')){
Dizzler
thanks i corrected it.
pixeline
@Dizzler - So, is it working for you?
pixeline
A: 

your hover code seems to run two functions one after the other which will effectively undo each others changes. so _off is turned to _on then back again.

so for hover to toggle it "on" you would just want

$("img.rollover").hover(function () {

this.src = this.src.replace("_off","_on");

});

if you just do this 1 function each time you can then control other states as you'd like e.g.

$("img.rollover").mouseout(function () {

this.src = this.src.replace("_on","_off");

});

metismo
hover does exactly that.. take two functions as parameters and bind each on the mouseover and mouseout events.. it is just a shortcut..
Gaby
A: 

You could unbind the hover event $(this).unbind('mouseenter mouseleave') when a click occurs, and rebind if necessary later.

tbreffni
binding/unbinding the events for all selected elements on every click on any of those elements seems really unefficient to me.
pixeline