Automatically add ALT attribute for all images using jQuery

Last Updated: January 28, 2019

var entryContentImages = $('img');
	
entryContentImages.each(function(){
   
	
	var alt = $(this).attr('alt');
	var title = $(this).attr('title');
	
	if (typeof alt === 'undefined'){
		$(this).attr('alt', $(this).attr('title'));
		console.log('ALT');
	}
	
	if (typeof title === 'undefined'){
		$(this).attr('title', $(this).attr('alt'));
		console.log('TITLE');
	}
	
	if (typeof title === 'undefined' && typeof title === 'undefined'){
		console.log('No Alt and Title Tags: ' + $(this).attr('src'));
		$(this).attr('alt', 'Image');
		$(this).attr('title', 'Image');
	}
});

Add your feedback or comment below: