jQuery Set Image TITLE from ALT Tag

Sam Deering

jQuery Code Snippet to get the alt tags of each image and set the title tag so it is the same as the alt tag. Could be useful to improve SEO for the web page and added support for browsers such as Firefox that utilise image title tags.

jQuery(document).ready(function($) {
	$("img:not([title])").each(function() {
			if($(this).attr("alt") != '') $(this).parent().attr("title", $(this).attr("alt"))
			else $(this).parent().attr("title", $(this).attr("src"));
	});
});

Free book: Jump Start HTML5 Basics

Grab a free copy of one our latest ebooks! Packed with hints and tips on HTML5's most powerful new features.

No Reader comments

Comments on this post are closed.