Home  >  

jQuery Quickie - Images and Alt Tags

Author photo
AddThis Social Bookmark Button
I had an interesting thought today (don't ask where, but it was the usual place where I get interesting ideas) - it really bugs me that Firefox doesn't show the ALT tag of an image as a tooltip. I get it that the TITLE tag is more "proper", and I don't want to get into a discussion of standards versus, oh, say practicality, but the problem remains the same. You may have old HTML content stored in a database that makes use of the ALT tag. What if you wanted to automatically update them to support the TITLE attribute? I spent a few minutes with jQuery today and found it had no problems with this. First, let's look at some simple HTML:

<img src="images/accept.png" alt="Accept"><br />
<img src="images/application_error.png" alt="Error" title="Error"><br />
<img src="images/accept.png" alt="Accept"><br />
<img src="images/application_error.png"><br />

I've got 4 images here. The first has an ALT attribute, but no TITLE. The second has both an ALT and a TITLE. The third is just a copy of the first. The last has no ALT or TITLE.

I began looking at jQuery selectors to see if it was possible to find all images that were missing a TITLE attribute. This can be done with the following selector:

img:not([title])

Reading left to right we have: All img tags NOT having title as an attribute.

I tested this selector by simply adding a border around them:

$("img:not([title])").css("border","3px solid red")

That worked like a charm. Now I needed to modify it so that I could set the TITLE attribute to the ALT value. A follower on Twitter suggested using the each operator:

$("img:not([title])").each(function() {
 $(this).attr("title", $(this).attr("alt"))
})

This worked like a charm, but obviously, only worked in cases where the ALT attribute existed. Many times the filename of the image can give you a clue as to what the image does. I don't know about you, but I will often run across web apps that make use of tiny icons (with no title attribute) that leave me scratching their head as to their actual purpose. I modified the code a bit to use the SRC attribute when ALT was empty.

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

This worked like a charm on my fourth image. Once again - jQuery amazes me. I've included the entire HTML for my example below.

<html>

<head>
<script src="/jquery/jquery.js"></script>
<script>

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

</script>
</head>

<body>

<img src="/images/accept.png" alt="Accept"><br />
<img src="/images/application_error.png" alt="Error" title="Error"><br />
<img src="/images/accept.png" alt="Accept"><br />
<img src="/images/application_error.png"><br />

</body>
</html>

Read more from Raymond Camden. Raymond Camden's Atom feed cfjedimaster on Twitter

Comments

6 Comments

John Blanco said:

Neat idea. I'd just run into that Firefox cause the other day and didn't realize it's just a FF thing. One question I'd have is why fall back to using src as a tooltip? It might depend on a particular use case, but I'd rather keep that if() and lose the else. :-)

The idea to use the src came from the fact that - sometimes - the name of the image is a hint as to what it does. So for example, an icon named user-deactivate.png - that tells me the icon does a deactivation event.

John Blanco said:

How about some formatting? Here's some quick code for the else statement:

function imgSrcToLabel(src) {
result = /([^\\\/]+)\.(png|jpg|gif)?$/.exec(src);

if (result) {
return result[1].replace(/[-_]/, " ");
}

return src;
}

So, the image http://raptureinvenice.com/images/user-delete.png would turn into title text user delete and if we used a capitalization routine User Delete.

Could be quite handy, I like your thought process.

My only concern would be - as a file path, I know in my head that it may not be the REAL purpose of the image, whereas the prettier version may imply more than it really means.

Then again, I'm probably over thinking it. ;) Cool mod!

Cody Marquart said:

This can be re-written two different ways with just jQuery, removing the if block

$("img:not([title])")
	.filter(":not([alt])").each(function(){$(this).attr("title",$(this).attr("src"))}).end()
	.filter("[alt!='']").each(function(){$(this).attr("title",$(this).attr("alt"))});

-or-
$("img:not([title]):not([alt])").each(function(){$(this).attr("title",$(this).attr("src"))});
$("img:not([title])[alt!='']").each(function(){$(this).attr("title",$(this).attr("alt"))});
Dr. Righteous said:

This isn't a problem with FireFox, it's a problem with Internet Explorer. ALT text should never be shown to anyone except for those unable to view the images. However, instead of the proper TITLE text in IE, you get ALT text.

It would be so nice if Micro$oft would follow W3C standards, instead of trying to create their own.

Leave a comment


Tag Cloud

Question of the Week: Dream App

If you had an unlimited budget and unlimited resources what application would you build and why would you build it?

Answer

Latest Features

Recommended for You

@InsideRIA on Twitter

Archives

  • Or, visit our complete archive.  

About This Site

Welcome to the premiere community site for all things RIA sponsored by O'Reilly Media and Adobe Systems Incorporated.