I have many image elements and want to get a specific image's source where the alternative text is "example".
I tried this:
var src = $('.conversation_img').attr('src');
but I can't get the one I need with that.
How do I select a specific image element based on its alternative text?
This question is related to
jquery
jquery-selectors
$('img.conversation_img[alt="example"]')
.each(function(){
alert($(this).attr('src'))
});
This will display src attributes of all images of class 'conversation_img' with alt='example'
If you do not specifically need the alt text of an image, then you can just target the class/id of the image.
$('img.propImg').each(function(){
enter code here
}
I know it’s not quite answering the question, though I’d spent ages trying to figure this out and this question gave me the solution :). In my case I needed to hide any image tags with a specific src.
$('img.propImg').each(function(){ //for each loop that gets all the images.
if($(this).attr('src') == "img/{{images}}") { // if the src matches this
$(this).css("display", "none") // hide the image.
}
});
var src = $('img.conversation_img[alt="example"]').attr('src');
If you have multiple matching elements only the src of the first one will be returned.
Source: Stackoverflow.com