Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get image src with jQuery

<img src="../img/arnold.png" alt="Arnold"> 

How do I get with jQuery absolute path of this image?

img.attr("src") gives me just "../img/arnold.png", should give something like "http://site.com/data/2011/img/arnold.png" (full url).

like image 822
Jasper Avatar asked Aug 05 '11 06:08

Jasper


People also ask

How to get src of an image in jQuery?

click(function(){ var images = $('. img1 img'). attr(src); alert(images); });

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.

What is the value of src attribute?

The <img> src attribute is used to specify the URL of the source image. Attribute Values: It contains single value URL which specifies the link of source image.


2 Answers

alert( $('img')[0].src ); 

this might do the trick... but not sure about cross browser....

demo in here

also try prop of jQuery 1.6..

alert( $('img').prop('src') ); 

demo here

like image 66
Reigel Avatar answered Sep 28 '22 02:09

Reigel


I don't know that you can get it with jQuery, but you can get it with just the native JavaScript image object.

var getSrc = function(imgSource) {     var img = new Image();     img.src = imgSource;     return img.src; }; 

Just call it with x = getSrc(srcAttribute) or something similar where your parameter is the string or literal holding the src you currently have in your html/image. It will return something like http://your/site/path/to/image.jpg

http://jsfiddle.net/BradleyStaples/cQMjQ/

like image 37
Bradley Staples Avatar answered Sep 28 '22 02:09

Bradley Staples