Search code examples
jqueryimagesource

Get Image Src from image title


Hi i have strange requirement of getting image src from image title using jquery. In my case image title are all different so there is no possibility of same image title.

Let me clarify the question. Here is one image tag.

<img title="Lion2" alt="Lion2" src="http://localhost/test/img/co/122.jpg" class="awp_pcp_square_img">

i am getting title Lion2.

Now i what i want is get src of this image tag. I can not do this by class as there are multiple image tag with the same class name.


Solution

  • You can use Attribute Equals Selector [name="value"] to get element by any attribute and use attr() to get the src value.

    Live Demo

    $('img[title=Lion2]').attr('src');
    

    Description: Selects elements that have the specified attribute with a value exactly equal to a certain value.

    You can also use indexer [] to get the DOM element are directly access the attribute value without using attr()

    $('img[title=Lion2]')[0].src;