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?
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');
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With