Update: I spoke your word βclickβ literally, which was a little dumb. You can replace focus with click with everything below, if you also want the action to happen when the user inserts a tab on the input, which seems likely.
Update 2: I assume you want to make placeholders; see note and example at the end.
Original answer :
You can do it:
$("selector_for_the_input").click(function() { this.value = ''; });
... but it will clear the text no matter what it is. If you want to clear it only if this value is:
$("selector_for_the_input").click(function() { if (this.value === "TEXT") { this.value = ''; } });
So, for example, if the input has id , you can do:
$("#theId").click(function() { if (this.value === "TEXT") { this.value = ''; } });
Or if it is in the form with id (say, "myForm"), and you want to do this for each field of the form:
$("#myForm input").click(function() { if (this.value === "TEXT") { this.value = ''; } });
You can also do this with a delegation:
$("#myForm").delegate("input", "click", function() { if (this.value === "TEXT") { this.value = ''; } });
This uses delegate to attach a handler in the form, but apply it to the inputs in the form, rather than hooking a handler for each individual input.
If you are trying to make placeholders, there is more than that, and you might want to find a good plugin for this. But here are the basics:
HTML:
<form id='theForm'> <label>Field 1: <input type='text' value='provide a value for field 1'> </label> <br><label>Field 2: <input type='text' value='provide a value for field 2'> </label> <br><label>Field 3: <input type='text' value='provide a value for field 3'> </label> </form>
JavaScript using jQuery:
jQuery(function($) { // Save the initial values of the inputs as placeholder text $('#theForm input').attr("data-placeholdertext", function() { return this.value; }); // Hook up a handler to delete the placeholder text on focus, // and put it back on blur $('#theForm') .delegate('input', 'focus', function() { if (this.value === $(this).attr("data-placeholdertext")) { this.value = ''; } }) .delegate('input', 'blur', function() { if (this.value.length == 0) { this.value = $(this).attr("data-placeholdertext"); } }); });
Live copy
Of course, you can also use the new placeholder attribute from HTML5 and execute only the above if your code works in a browser that does not support it, in which case you want to invert the logic used above:
HTML:
<form id='theForm'> <label>Field 1: <input type='text' placeholder='provide a value for field 1'> </label> <br><label>Field 2: <input type='text' placeholder='provide a value for field 2'> </label> <br><label>Field 3: <input type='text' placeholder='provide a value for field 3'> </label> </form>
JavaScript with jQuery:
jQuery(function($) { // Is placeholder supported? if ('placeholder' in document.createElement('input')) { // Yes, no need for us to do it display("This browser supports automatic placeholders"); } else { // No, do it manually display("Manual placeholders"); // Set the initial values of the inputs as placeholder text $('#theForm input').val(function() { if (this.value.length == 0) { return $(this).attr('placeholder'); } }); // Hook up a handler to delete the placeholder text on focus, // and put it back on blur $('#theForm') .delegate('input', 'focus', function() { if (this.value === $(this).attr("placeholder")) { this.value = ''; } }) .delegate('input', 'blur', function() { if (this.value.length == 0) { this.value = $(this).attr("placeholder"); } }); } function display(msg) { $("<p>").html(msg).appendTo(document.body); } });
Live copy
(Kudos to diveintohtml5.ep.io for placholder function detection code .)