views:

43

answers:

2
$('.icon1').mouseover(function(){
   $(this).find('img').attr('src', 'recursos/botban/maq1.png');
});

Should work right? It's just an arbitrary test to see what was wrong, but it's still broken.

I've also tried with $('.icon1').hover(function(){..., and it also does not work.

What I really want is more like...

$('.icon1').mouseover(function(){
        var alt = $(this).find('img').attr('src')+'.png';
        $(this).find('img').attr('src', $(this).attr('id')+''.png);
}).mouseout(function(){
        $(this).find('img').attr('src', alt);
});

The HTML for each image is as follows...

<a class='icon1'><img src='recursos/botban/veh1.png'></a>
+3  A: 

A few things, I'd use mouseenter instead of mouseover (since mouseout will fire when entering a child), and also make sure it's running inside a document.ready handler, like this:

$(function() {
  $('.icon1').each(function(){
    $.data(this, 'src', $(this).find('img').attr('src'));
  }).hover(function(){
    $(this).find('img').attr('src', this.id + '.png'); //may need adjustment
  }).mouseout(function(){
    $(this).find('img').attr('src', $.data(this, 'src'));
  });
});

I'm not sure exactly what hover image you want, but the general approach is to store what it was originally and use than then restoring it on mouseleave. Or, just put the hover on the <img> itself, like this:

$(function() {
  $('.icon1 img').each(function(){
    $.data(this, 'src', this.src);
  }).hover(function(){
    this.src = 'otherImage.png';
  }).mouseout(function(){
    this.src = $.data(this, 'src');
  });
});
Nick Craver
It's always been running inside a document.ready, but that gives me nothing :/
serv-bot 22
@serv-bot - What *is* it doing...and are you loading this `.icon1` dynamically, e.g. via ajax?
Nick Craver
The .icon1 are not being loaded dynamically. It's really doing nothing; the image stays static.
serv-bot 22
The images are also within a div box, although I really don't think that matters.
serv-bot 22
@serv-bot - Are there any JavaScript errors on the page? Check the console, sounds like this isn't being run at all, at least not without error.
Nick Craver
The JS is run from within the only JS file I use on all the pages of the site. It runs ok on all other pages.
serv-bot 22
Oh dear, I forgot to import JQuery on this particular page. I feel really stupid now! Thanks Nick
serv-bot 22
@serv-bot - Ah there we go, welcome :)
Nick Craver
A: 
(function($) {  // private closure;  
    $(function() {  // document load;
        $('.icon1').hover(function(){
            $(this).find('img').attr('src', "recursos/botban/veh1.png");
        }, function(){
            $(this).find('img').attr('src', "recursos/botban/veh2.png");
        });
 });
})(jQuery);
jerone
Sorry, I had already tried this and it does not work. I stated that in my post.
serv-bot 22
are you sure these images do exist (in that path too)?
jerone
Even if they didn't, the browser should at least show the broken image symbols.
serv-bot 22