Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery get the image src

Tags:

jquery

People also ask

How to get image src jQuery?

To get the source of an image, we can use jQuery to get the img src using the attr() method. The jQuery attr method will return the location of the file as a string.

How add src attribute in jQuery?

Answer: Use the jQuery attr() Method You can use the attr() method to change the image source (i.e. the src attribute of the <img> tag) in jQuery. The following example will change the image src when you clicks on the image.

How can add image in HTML using jQuery?

With jQuery, you can dynamically create a new image element and append it at the end of the DOM container using the . append() method. This is demonstrated below: jQuery.


src should be in quotes:

$('.img1 img').attr('src');

for full url use

$('#imageContainerId').prop('src')

for relative image url use

$('#imageContainerId').attr('src')

function showImgUrl(){
  console.log('for full image url ' + $('#imageId').prop('src') );
  console.log('for relative image url ' + $('#imageId').attr('src'));
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<img id='imageId' src='images/image1.jpg' height='50px' width='50px'/>

<input type='button' onclick='showImgUrl()' value='click to see the url of the img' />

You may find likr

$('.class').find('tag').attr('src');

When dealing with the HTML DOM (ie. this), the array selector [0] must be used to retrieve the jQuery element from the Javascript array.

$(this)[0].getAttribute('src');

This is what you need

 $('img').context.currentSrc

In my case this format worked on latest version of jQuery:

$('img#post_image_preview').src;