How to get image attribute (height, width, alt, title)

How to get image attribute (height, width, alt, title) when knowning image src.

Create an img object and give us your image src, then once it has loaded get the image attributes that you need from it.

If you need to work with one that’s already on the page, but you don’t know where it is, search for all of the img tags on the page than filter them for the one that has a matching src.

Yep. If you’re using jquery you can get it like this:


var image_src = "foo.png"
$( 'img[src="' + image_src + '"]' );

It would be a little more involved with straight javascript though. The image has to actually be loaded in order for you to know it’s dimensions, at least with javascript.