Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you swap an input text field to a password area on focus in JQuery?

I would like to display "password" as text in the password area, and when focused the box should become empty and allow normal password (starred out) input from the user.

Currently I use the following method. Initially I display a text field showing password, when focused it is removed and replaced with a password field. Here is the jQuery:

$(document).ready(function() {
    $("#password").focus( function() {
        $("#pwd").html('<input type="password" id="password" name="password" value="" />');
        $("#password").focus();
    });
});

This works on Firefox and Chrome but fails in IE 8 (and presumably other IE's) as the focus call fails for some reason (maybe the DOM isn't ready?).

A demo is available on jsbin.

Any ideas?

like image 969
Pool Avatar asked Jun 17 '09 23:06

Pool


People also ask

How can input focus in jQuery?

Using jQuery With jQuery, you can use the . focus() method to trigger the “focus” JavaScript event on an element. This method is a shortcut for . trigger("focus") method.

How do you focus text input?

Input Text focus() Method The focus() method is used to give focus to a text field. Tip: Use the blur() method to remove focus from a text field.

What is focus function in jQuery?

jQuery focus() Method The focus event occurs when an element gets focus (when selected by a mouse click or by "tab-navigating" to it). The focus() method triggers the focus event, or attaches a function to run when a focus event occurs. Tip: This method is often used together with the blur() method.

How do you focus on the input field when the page is loaded?

The autofocus attribute is a boolean attribute. When present, it specifies that an <input> element should automatically get focus when the page loads.


1 Answers

You could try the watermarker plugin. I don't know if it works in IE8.

like image 172
eKek0 Avatar answered Oct 27 '22 00:10

eKek0