1

In the code below, I would like to use jQuery to put the word "username" into the value field. Then, when the user selects the input box, the word "username" would disappear leaving an empty input field for the user to input his username.

Can this be done with jQuery?

<p class="login-username">
    <input type="text" name="log" id="user_login" class="input" value="" size="20" tabindex="10">
</p>
1
  • 1
    What about HTML placeholder attribute?. Then use a polyfill for old browsers. Done. Commented Nov 20, 2012 at 0:57

8 Answers 8

4

You can use HTML5 placeholder for that

<input type="text" placeholder="Username" />

And fix it for older browsers ( that's the exact part you were asking )

$('[placeholder]').focus(function() {
  var input = $(this);
  if (input.val() == input.attr('placeholder')) {
    input.val('');
    input.removeClass('placeholder');
  }
}).blur(function() {
  var input = $(this);
  if (input.val() == '' || input.val() == input.attr('placeholder')) {
    input.addClass('placeholder');
    input.val(input.attr('placeholder'));
  }
}).blur();

SOURCE


UPDATE

To alter the HTML via jQuery and add a placeholder to an input field you can do this

$("input").prop("placeholder", "username");

DEMO

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you, but I was hoping you could provide me a way to insert the 'placeholder' attribute into the html using jQuery as I don't want to mess with any core files.
^ That defeats the purpose of asking for html5 then if you don't want to use any html.
@John updated my answer - it's really easy to add the placeholder via jQuery
@Bankzilla I've added the HTML tag because of the placeholder but it turned out that the OP doesn't want to change his HTML, so just deleted the tag
Was referring to the OP, your answer is spot on +1.
1
$("#user_login").val("username");


$("#user_login").focus(function(){
    $(this).val("");
});

Working example: http://jsfiddle.net/jbB35/

Comments

1

The placeholder attribute is probably what you want, but to answer your question:

$(".login-username input").val("username");
$(".login-username input").on("focus", function() {
  if($(this).val() == "username") {
    $(this).val("");
  }
}).on("blur", function() {
  if($(this).val() == "") {
    $(this).val("username");
  }
});

4 Comments

-1, what happens if the control gains focus and then loses it again? The default text only works once.
If you bind to every focus event, what you've input previously will be cleared. That was why I went with the .one() binding.
My point is that if the user puts focus on the field, and then removes it without entering text, the placeholder text will not return. This is not a good solution.
My apologies. I posted hurriedly and it was the bare minimum. My edit fixes that.
0

You don't need jQuery for this. You could just use the placeholder attribute.

From Dive into HTML5:

The first improvement HTML5 brings to web forms is the ability to set placeholder text in an input field. Placeholder text is displayed inside the input field as long as the field is empty. When you click on (or tab to) the input field and start typing, the placeholder text disappears.

http://diveintohtml5.info/forms.html#placeholder

Comments

0

With HTML5 you can simply set placeholder="username" and modern browsers will handle this automatically. To catch the stragglers, there are many shim or shiv scripts such as this one or this one that provide placeholder support for non-HTML5-compliant browsers.

Comments

0

If you use XHTML

var user_login_text="Username";
$("#user_login").val=user_login_text;
$("#user_login").focus(function(){
    $(this).val("");
}).blur(function(){
    $(this).val(user_login_text);
});

Comments

0

Option 1: jQuery Plugin (or write it yourself)

There are several default text plugins to choose from. I'm partial to this one: http://www.examplet.buss.hk/jquery/defaultText.php.

Option 2: HTML5 placeholder attribute

Utilize the placeholder attribute (HTML5). Using placeholder on supported browsers will give you the functionality you desire.

<input type="text" id="user_login" placeholder="username" />

If you choose to go the placeholder route, you can style your default text like this:

input::-webkit-input-placeholder {
    color:    #999;
}
input:-moz-placeholder {
    color:    #999;
}
input:-ms-input-placeholder {
    color:    #999;
}

See this SO question for additional details.

Note: Be aware that placeholder is not supported in all browsers. You can view supported browsers here.

Comments

0

You can just use simple javascript:

<p class="login-username">
<input type="text" name="log" id="user_login" class="input" value="Username" size="20" tabindex="10" onfocus="if(!this._haschanged){this.value=''};this._haschanged=true;">
</p>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.