How to add title from alt text on all images using jQuery!

This article was written by in July 27, 2014, & may not be posted on other sites!
Original source url for this article: How to add title from alt text on all images using jQuery!

How to add title from alt text on all images using jQuery!
This is how to take the alt text attribute from all images and add the same text in the title attribute. You can replace the title text or add the title text if it’s missing. You can also do the opposite, take the title text and use it to add alt text on all images. This is done dynaically with a simpe jQuery function.

From this:
<img alt="my beautiful image" src="images/myimg.jpg">

To this:
<img alt="my beautiful image" title="my beautiful image" src="images/myimg.jpg">

 

How to add title from alt text on all images using jQuery

This function is taking the alt text of each image and then adding the title attribute using the same text. This function will also write over any existing title text.
$("img").each(function() {
var imgAlt = $(this).attr('alt');
$(this).attr("title", imgAlt);
});

 

How to add alt from alt title on all images using jQuery

This function is taking the title text of each image and then adding the alt attribute using the same text. This function will also write over any existing alt text.
$("img").each(function() {
var imgTitle = $(this).attr('title');
$(this).attr("alt", imgTitle);
});

 

Spread the word

Facebooktwittergoogle_pluslinkedinmail

One thought on “How to add title from alt text on all images using jQuery!

Leave a Reply

Your email address will not be published. Required fields are marked *