The following Javascript will clear default text value in input boxes when you click on them. To trigger the Javascript, you'll need to specify both title and value attributes and set class of the input field to "text". jQuery library is required for this to work.
Javascript
$('.text').focus(function () { if ($(this).val() == $(this).attr("title")) { $(this).val(""); } }).blur(function () { if ($(this).val() == "") { $(this).val($(this).attr("title")); } });
Examples:
<input type="text" name="q" class="q text" value="Search for terms" title="Search for terms" /> <input type="password" name="password" class="text" value="Password" title="Password" />
Is this not 100x more simpler? :)
.. http://www.webdeveloper.com/forum/showthread.php?t=143399
Hi James,
Your example does look simpler but it’s not an ideal solution because Javascript is not accessed via DOM and it will require you to put the same piece of code multiple times if you want to apply it to more than one input box.
Alternate Method :-
==============
$(document).ready(function(){
$(“#btnclear”).click(function()
{ $(‘:text’).val(“”); });
});
====
#btnclear is the id of the button
$(document).ready(function(){
$(“#btnclear”).click(function()
{ $(‘:text,textarea’).val(“”); });
});
this will clear text area too