peehskcalba peehskcalba - 1 year ago 50
jQuery Question

How to use jQuery to convert text to email links

I have an HTML table with one column being email addresses; spambot issues aside, I'd like to convert each

element in this column to a mailto: link.

Here's what I've got:

$(" tbody tr td:last").each(function() {
var email = $(this).val();
$(this).html('<a href="mailto:' + email + '">' + email + '</a>');

And this is the table (sample):


What's going wrong?

Answer Source

Your jquery selector is wrong.

When you do ' tbody tr td:last' it will only select the last cell of the last row.

What you need to do is something like:

        //For each row...
        $(' tbody tr').each(
                //...find the last cell.
                var lastCell = $(this).find('td:last');
                $(lastCell).html('<a href="mailto:' + $(lastCell).text() + '">' + $(lastCell).text() + '</a>');

Working demo: Code: