Piyush
Piyush

Reputation: 13

Issue with JQuery Each function

I have a jquery function to hide email ids on the webpage to avoid spambots. I am trying to replace all the span tags with class 'mailme' to valid email ids with the help of this function. The code was working for 1 span tag but since its changed to multiple spans with the help of each method, its not working.

Html

<span class="mailme">myemail at mydomain dot com</span>

jQuery

$('span.mailme').each(function(){
var spt = "#" + $(this).attr("id");
var at = / at /;
var dot = / dot /g;
var addr = $(spt).text().replace(at,"@").replace(dot,".");
$(spt).after('<a href="mailto:'+addr+'" title="Send an email">'+ addr +'</a>')
.hover(function(){window.status="Send a letter!";}, function(){window.status="";});
$(spt).remove();
});

Upvotes: 0

Views: 329

Answers (1)

Šime Vidas
Šime Vidas

Reputation: 185893

Live demo: http://jsfiddle.net/g7Szt/2/

$('span.mailme').each(function () {
    var addr = $(this).text().replace(/ at /, "@").replace(/ dot /g, "."),
        s = '<a href="mailto:' + addr + '" title="Send an email">' + addr + '</a>',
        link = $(s);

    link.hover(function () {
        window.status = "Send a letter!";
    }, function () {
        window.status = "";
    });

    $(this).replaceWith(link);
});

<head>
    ...
    <script>
        $(function() {
            // place all jQuery code here
        });
    </script>
    ...
</head>

Upvotes: 3

Related Questions