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.
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.
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;
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