Baruch
Baruch

Reputation: 2428

jquery rollover question

i made an image roll over (first one i make with out tutorials) and i want to know if there is a way to kill the script if the one image ends with over.jpg and make script continue if the image doesn't

this is my code

$('#topNav a img').hover(function(){
    var rSrc = $(this).attr('name');
    $(this).attr('src','images/' + rSrc + 'over.jpg');
}, function(){
    var rSrc = $(this).attr('name');
    $(this).attr('src', 'images/' + rSrc + '.gif');
});

html

    <a href="index.html" class="nav"><img src="images/m_1over.jpg" name="m_1"/></a>
<a href="aboutus.html" class="nav"><img src="images/m_2.gif" name="m_2"/></a>

i tried adding

if($('img[src$=.gif]')

before the script but it doesn't work...

Upvotes: 4

Views: 101

Answers (3)

Alex Pakka
Alex Pakka

Reputation: 9706

You mean, you need something like this?


$('#topNav a img').each(function(i, el) {
   if ($(el).attr('src').match('\\.gif$') == '.gif') 
   {
           $(el).hover(function(){
              var rSrc = $(this).attr('name');
              $(this).attr('src','images/' + rSrc + 'over.jpg');
           }, 
           function(){
              var rSrc = $(this).attr('name');
              $(this).attr('src', 'images/' + rSrc + '.gif');
           });
   }
}

You can play with the selector here, where I tested the code: http://jsfiddle.net/Exceeder/NE3Ps/

Upvotes: 0

Sarfraz
Sarfraz

Reputation: 382696

You can use the not() method like this:

$('#topNav a img').not('#topNav a img[src=over.jpg]').hover(function(){...}

Upvotes: 2

karim79
karim79

Reputation: 342635

How about this:

$('#topNav a img').not("[src$=over.jpg]").hover(function(){ ...

See http://api.jquery.com/not/

Upvotes: 2

Related Questions