Q:

Insert image after a link

I have a large table that one column contains e-mail addresses (+/- 60), and I was told to add an e-mail icon after each address. I have been trying to use anchors,.href and .indexof to select the e-mail links, but have gotten no where (hence no code).
Can I get assistance on how to add <img src="/a/path/email.jpg" alt="e-mail" /> if the link's href contains a mailto using vanilla JavaScript, no jQuery.

A:

Here's a function you could use:
(function(allLinks)
{
    var i, imgHTML = '<img src="mail.png" alt="mail" />';
    for (i = 0; i < allLinks.length; i++)
    {
        if (allLinks[i].getAttribute('href').indexOf('mailto') > -1)
        {//or allLinks.item(i).getAttribute('href').indexOf('mailto')
         //calling the item method is said to be faster, on most browsers
            allLinks[i].innerHTML += imgHTML;
        }
    }
}(document.getElementsByTagName('a')));

You can, of course turn this IIFE into a regular function, to be called on the load event or something...
Forked @adardesign's fiddle, here it is

