user120944
user120944

Reputation:

jQuery Text Masking

I'm trying to create a small snipped that would allow for text to be masked unless a link was hovered over it. Something like this:

<span class="mask">My Information</span>
<a href="#">View Info</a>

When the page loads, the page is displayed as follows:

<span class="masked">••••••••••••••</span>
<a href="#">View Info</a>

Essentially the bullets would take up as much space as the text that its replacing. Then when a user clicks the "View Info" link, the mask would revert back to the information.

<span class="mask">My Information</span>
<a href="#">View Info</a>

There will be multiple instances of this on one page.

Any ideas? I don't really know where to begin.. I was thinking about making a bunch of password fields but thought it would get messy..

Upvotes: 0

Views: 3364

Answers (3)

Mina Gabriel
Mina Gabriel

Reputation: 25070

I know this might not related to the question 'but here is how to show and hide password' using jquery

  $(function(){
        $('#showPassword').change(function(){
        var passwordText = $('#password');
        if($(this).prop('checked') == true){
            passwordText.get(0).type = 'text';
        }else{
            passwordText.get(0).type = 'password';
        }
     });
 });

HTML

    userName :<input type="text" id="strUserName" name="strUserName"/>
    password:<input type="password" id="password" name="password" />
    show Password<input type="checkbox"  id="showPassword"  />

Upvotes: 0

Paul Creasey
Paul Creasey

Reputation: 28824

far from perfect i'm sure!

jsfiddle

(function($) {

    $.fn.toggleMask = function() {

        this.each(function() {
            if ($(this).hasClass('masked')) {
                $(this).text($(this).attr('origValue'));
                $(this).removeClass('masked');
                var link = $(this).next();
                link.text(link.text().replace(/Show/,'Hide'));
            }
            else {
                $(this).attr('origValue', $(this).text());
                $(this).text(new Array($(this).text().length).join('•'));
                $(this).addClass('masked');
                var link = $(this).next();                
                link.text(link.text().replace(/Hide/,'Show'));
            }
        });

    return this;
};
})(jQuery);

$(function() {
    $('.mask').toggleMask();
    $('a').click(function() {
        $(this).prev().toggleMask();
        return false;
    });
});

Upvotes: 1

vad
vad

Reputation: 1266

Use data(). At load you can put the content of every $('.mask') in a data:

$('.mask').each(function(){
    var t = $(this)
        ,text = t.text();
    t.data('text', text);
    //replace text with stars
});

And then you can bind a function to the mouse click that replaces stars with text.

Upvotes: 0

Related Questions