Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hiding Email Address With jQuery

Tags:

jquery

email

I know there are plugins that do this, but I need something pretty specific and I can't find anything.

I need a jQuery script that only change the mailto link, and not the text of the link. For example:

<a href="person(at)exammple.com">Person's Email</a>

I need to display something else inside the link besides the actual e-mail address. The script I was trying to use is from this site.

Here's the script:

jQuery.fn.mailto = function() {
    return this.each(function(){
        var email = $(this).html().replace(/\s*\(.+\)\s*/, "@");
        $(this).before('<a href="mailto:' + email + '" rel="nofollow" title="Email ' + email + '">' + email + '</a>').remove();
    });
};

I think I need to replace the "email" variable with something else in between the <a> tags, but I'm not sure with what. I'm still pretty new to jQuery.

Thanks.

like image 796
JacobTheDev Avatar asked Sep 20 '11 18:09

JacobTheDev


2 Answers

How about this:

(function($) {
    jQuery.fn.mailto = function() {
        return this.each(function() {
            var email_add = $(this).attr("href").replace(/\s*\(.+\)\s*/, "@");
            var email_text = $(this).html();
            $(this).before('<a href="mailto:' + email_add + '" rel="nofollow" title="Email ' + email_add + '">' + email_text + '</a>').remove();
        });
    };

})(jQuery);

$(document).ready(function() {
    $(function() {
        $('.email').mailto();
    });
});

You can try it @ jsfiddle

like image 125
Ujjwal Manandhar Avatar answered Oct 21 '22 05:10

Ujjwal Manandhar


var email = $(this).html().replace('@', '(at)');

like image 36
Royi Namir Avatar answered Oct 21 '22 03:10

Royi Namir