2

I have some onfocus/onblur handlers for a field that clears the default contents when focused, but only if the value equals the default value (so as to not remove any user input).

The field can also toggle between inputs. So rather than accepting a username, the user could click a button and it would accept an email address. This would require a different default value, such as "enter your email address". The actual property attribute defaultValue needs to be changed to allow my focus/blur handlers to work properly, using something like this:

$('input#ident-field').prop('defaultValue', 'enter your email address');

This doesn't seem to work because if you focus the field when it asks for an email address it doesn't clear it.

3
  • read about .prop() : It should be used when setting selectedIndex, tagName, nodeName, nodeType, ownerDocument, defaultChecked, or defaultSelected. Commented Apr 25, 2012 at 6:15
  • So not defaultValue? Either way, I've tried .attr() and .prop() and neither are working. Commented Apr 25, 2012 at 6:17
  • see my answer; if still you wants to go with jQuery then use .data() instead Commented Apr 25, 2012 at 6:20

2 Answers 2

4

Your shoud use HTML 5 attribute placeholder

<input type="text" placeholder="Enter Your email address" / >
Sign up to request clarification or add additional context in comments.

1 Comment

You'll also want to add github.com/mathiasbynens/jquery-placeholder to make it work in older browsers.
0

You can do this with the use of focus and blur functions:

$('input#ident-field').val("enter your email address");
// or $('input#ident-field').prop('defaultValue', 'enter your email address');
$('input#ident-field').focus(function(){
    $(this).val("");
});
$('input#ident-field').blur(function(){
    if($(this).val().trim() == "")
    {
        $(this).val("enter your email address");
    }
});

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.