Tuesday, May 22, 2012

Add mailto link to static email with JQuery

I'm trying to add a mailto link to static email addresses found within a list of results from a database, using JQuery. I was able to find the following excerpt online which works for the first result, but it does not work for any email addresses after the first one.



I'm curious why this is.. and how I can get it to apply the mailto: attribute to every email address found in the results. :-)



Current code:



<script type="text/javascript">
$(document).ready(function(){
var regEx = /(\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)/;
$("table td").filter(function() {
return $(this).html().match(regEx);
}).each(function() {
$(this).html($(this).html().replace(regEx, "<a href=\"mailto:$1\">$1</a>"));
});
});




Thanks!





No comments:

Post a Comment