Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jquery: Fix for no placeholder tag and password fields #3

Open
mordy opened this issue Jun 8, 2012 · 0 comments
Open

Jquery: Fix for no placeholder tag and password fields #3

mordy opened this issue Jun 8, 2012 · 0 comments

Comments

@mordy
Copy link

mordy commented Jun 8, 2012

I thought i would share my fixes

$j(document).ready(function(){
(function($) {
$(function() {
if(!('placeholder' in document.createElement("input"))) { //don't do any work if we don't have to!
var inputs = $('input[type="text"][placeholder]');
var origColour = inputs.eq(0).css("color");

            inputs
                .each(function(i) {
                    var $t = $(this);
                    if($t.val()===''){
                        $t.val($t.attr("placeholder")).css("color", "#585858");
                    }
                })
                .focus(function() {
                    var $t = $(this);
                    if( $t.val() === $t.attr("placeholder")) $t.val('').css("color", origColour);
                })
                .blur(function() {
                    var $t = $(this);
                    if($t.val() === '') {
                        $t.val($t.attr("placeholder")).css("color", "#585858");
                    }
                });
            var $password_fields = $('input[type="password"][placeholder]');
            origColour = $password_fields.eq(0).css("color");

            $password_fields
                .each(function(i) {
                    var $t = $(this);

                    $copy = $('<input type="text">')
                        .val($t.attr("placeholder"))
                        .css("color", "#585858")
                        .hide()
                        .focus(function(){
                            $copy.hide();
                            $t.show().focus();
                        });
                    $t.after($copy);
                    $t.data('placeholder',$copy);
                    if($t.val()===''){
                        $t.hide();
                        $copy.show();
                    }
                })
                .blur(function() {
                    var $t = $(this);
                    if($t.val() === '') {
                        $copy = $t.data('placeholder');
                        $copy.show();
                        $t.hide();
                    }
                });
        }
    })

})(jQuery);

});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant