jQuery Wrap All Images on Page

Sam Deering
Tweet

Simple jQuery code snippet to wrap all images on a web page into a div which contains a class for formatting. The jQuery wrap() function can be used to wrap any elements on a page with any html code.

jQuery(document).ready(function($) {
	$("img").each(function() {
		$(this).wrap('<div class="someclass"></div>');
	}
});

Free JavaScript: Novice to Ninja Sample

Get a free 32-page chapter of JavaScript: Novice to Ninja and receive updates on exclusive offers from SitePoint.

No Reader comments