Get image size using jQuery
I have a list of images
<img src="001.jpg"> <img src="002.jpg"> <img src="003.jpg"> <img src="004.jpg"> <img src="005.jpg"> Each image has a width of 200 pixels, but the heights are different. Is there a way with jQuery to find and then set the height of each image after loading?
I plan to have dozens of images on one page and do not want to add the width and height attribute to each image tag.
I use the Masonry Plugin, and the images require the width and height attribute.
+4
4 answers
You can try:
$('img').each( function(){ var height = $(this).height(); var width = $(this).width(); $(this).attr({'height': height, 'width': width}); }) Assuming that the height / width attributes should be set to the actual img height / width, and not scaled / resized in any way.
Literature:
+3