24

When a web form is written to the browser, the browsers remembers what the initial values are of a text INPUT box. ie. when it receives HTML like this:

<input type="text" value="something">

The browser remembers "something" as the initial/default value. When the user starts typing over it, then hits ESC, the browser reverts the field to the initial value (or blank if it was initially blank of course).

However, when creating a text input box programatically, hitting ESC always seems to blank the box, even if I create it with a default value like so:

$('<input type="text" value="something">')

The browser doesn't count this as a default value and doesn't revert to it when hitting ESC. So my question is, is there a way to create a text box in code and somehow assign it a default value, so the ESC key works as if the browser received it in the HTML document?

JGallardo
  • 11,074
  • 10
  • 82
  • 96
ingredient_15939
  • 3,022
  • 7
  • 35
  • 55

4 Answers4

39

You might looking for the placeholder attribute which will display a grey text in the input field while empty.

From Mozilla Developer Network:

A hint to the user of what can be entered in the control . The placeholder text must not contain carriage returns or line-feeds. This attribute applies when the value of the type attribute is text, search, tel, url or email; otherwise it is ignored.

However as it's a fairly 'new' tag (from the HTML5 specification afaik) you might want to to browser testing to make sure your target audience is fine with this solution.
(If not tell tell them to upgrade browser 'cause this tag works like a charm ;o) )

And finally a mini-fiddle to see it directly in action: http://jsfiddle.net/LnU9t/

Edit: Here is a plain jQuery solution which will also clear the input field if an escape keystroke is detected: http://jsfiddle.net/3GLwE/

Solomon Ucko
  • 5,724
  • 3
  • 24
  • 45
nuala
  • 2,681
  • 4
  • 30
  • 50
  • 6
    Since no one has provided an example for this, add in HTML input: – newUserNameHere Aug 19 '13 at 13:51
  • Thanks, that could be useful, however I need something for the general public (ie. backward compatible) even if it's a javascript approach. – ingredient_15939 Oct 08 '13 at 08:20
  • @ingredient_15939 I'm sorry for the monster delay but I've added another fiddle which doesn't really on placeholder tag. I used data-placeholder for convenience but one could also define a JS variable instead. – nuala May 22 '14 at 16:43
8

This esc behavior is IE only by the way. Instead of using jQuery use good old javascript for creating the element and it works.

var element = document.createElement('input');
element.type = 'text';
element.value = 100;
document.getElementsByTagName('body')[0].appendChild(element);

http://jsfiddle.net/gGrf9/

If you want to extend this functionality to other browsers then I would use jQuery's data object to store the default. Then set it when user presses escape.

//store default value for all elements on page. set new default on blur
$('input').each( function() {
    $(this).data('default', $(this).val());
    $(this).blur( function() { $(this).data('default', $(this).val()); });
});

$('input').keyup( function(e) {
    if (e.keyCode == 27) { $(this).val($(this).data('default')); }
});
mrtsherman
  • 39,342
  • 23
  • 87
  • 111
  • The PlaceHolder attribute in Yoshi's answer is now widely available. I would use that approach vs this javascript approach. When I originally answered this in 2012, IE8 and IE9 were still in wide use and did not support placeholder. – mrtsherman Mar 03 '17 at 16:12
4

See the defaultValue property of a text input, it's also used when you reset the form by clicking an <input type="reset"/> button (http://www.w3schools.com/jsref/prop_text_defaultvalue.asp )

btw, defaultValue and placeholder text are different concepts, you need to see which one better fits your needs

Andreas Andreou
  • 1,332
  • 10
  • 10
4

If the question is: "Is it possible to add value on ESC" than the answer is yes. You can do something like that. For example with use of jQuery it would look like below.

HTML

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>

<input type="text" value="default!" id="myInput" />

JavaScript

$(document).ready(function (){
    $('#myInput').keyup(function(event) {
        // 27 is key code of ESC
        if (event.keyCode == 27) {
            $('#myInput').val('default!');
            // Loose focus on input field
            $('#myInput').blur();
        }
    });
});

Working source can be found here: http://jsfiddle.net/S3N5H/1/

Please let me know if you meant something different, I can adjust the code later.

Marek Tuchalski
  • 489
  • 2
  • 8
  • Thanks, I'm aware it can be done with some script, but I was hoping there was a property of the element that the browser natively supports as a default value - rather than duplicating it manually in script. However, it appears mrtsherman is right, and it's an IE-only thing, so - script it is I guess. :) – ingredient_15939 Jan 20 '12 at 09:14