Sunday, October 25, 2009

Clear default text onClick - restore if nothing entered

Let's write a JavaScript function which will clear the "default" text in a field when the user clicks into it and the second function will replace the default text in the field if the field was left blank.

Put this inside the <head> </head> tag:

<script type="text/javascript">
function clickclear(thisfield, defaulttext) {
if (thisfield.value == defaulttext) {
thisfield.value = "";
}
}

function clickrecall(thisfield, defaulttext) {
if (thisfield.value == "") {
thisfield.value = defaulttext;
}
}
</script>

Then add the following onclick, onblur events to your field:

<asp:TextBox ID="rajTextBox" runat="server" Text="Rajendra Sedhain"
Width="369px" Height="18px" onclick="clickclear(this, 'Rajendra Sedhain')" onblur="clickrecall(this,'Rajendra Sedhain')"></asp:TextBox>

Happy Programming !!!

No comments:

Post a Comment

Highly Appreciated your comments