Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

comparing two img src

Tags:

html

jquery

Okay so I have this function that gets the img src for every img with a class of visible:

$('.visible').each(function() {
        var img = ($(this).attr('src'));
          alert(img);
            });

now I need to compare the first two img src to see if they are the same i tried this:

   $('.visible').each(function() {
         var firstImg = ($(this).eq(0).attr('src'));
         var secondImg = ($(this).eq(1).attr('src'));

if(firstImg == secondImg){
alert('match');
}else{
alert('not a match'); 
}

            });

this is clearly wrong so how could i go about this?

like image 920
swsa Avatar asked Mar 19 '26 20:03

swsa


1 Answers

Inside the each handler this refers to a single image element, so $(this).eq(1).attr('src') will always return undefined

what you need is

var $imgs = $('.visible');

var firstImg = $imgs.eq(0).attr('src');
var secondImg = $imgs.eq(1).attr('src');

if(firstImg == secondImg){
    alert('match');
}else{
    alert('not a match'); 
}
like image 74
Arun P Johny Avatar answered Mar 22 '26 10:03

Arun P Johny



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!