Default text in text box on page load
Generally, I've seen any registration form filled with text fields, like Enter your first name or enter your last name here. How can I write like this. Is it javascript? can u tell me how can i write that like this help me thanks
Based on your comments on the post from phoenix, you can try something like this:
<input type="text" value="Enter your name..." onfocus="CheckText(this, 'Enter your name...');" />
<script language="javascript">
function CheckText(e, text){
if(e.value == text){
e.value = '';
}
}
</script>
You want TextBoxWatermark . AJAX management toolkit has one.
Also, here's an example of a custom ASP.NET Textbox watermark .
EDIT: Below is an example of the link given above.
Control markup:
<asp:textbox id="txtSimpleSearch" runat="server"></asp:TextBox>
JS functions:
function WatermarkFocus(txtElem, strWatermark) {
if (txtElem.value == strWatermark) txtElem.value = '';
}
function WatermarkBlur(txtElem, strWatermark) {
if (txtElem.value == '') txtElem.value = strWatermark;
}
Code for:
string strWatermarkSearch = "Search";
txtSimpleSearch.Text = strWatermarkSearch;
txtSimpleSearch.Attributes.Add("onfocus", "WatermarkFocus(this, '" + strWatermarkSearch + "');");
txtSimpleSearch.Attributes.Add("onblur", "WatermarkBlur(this, '" + strWatermarkSearch + "');");
As you suggested yourself, you can use JavaScript that is called on an event onload
, for example:
<body onload="document.getElementById('firstName').value = 'Enter first name'" ...>
You can group multiple statements in a function and just call it on onload
.
Or you can just set the value directly on the input:
<input type="text" id="firstName" name="firstName" value="Enter first name" />
You need to set the value of the property of the textbox to the text you want, like
<input type="text" id="txt1" value="Enter text here" />
When input takes focus and value is the default, then clear the value. And if the user hasn't entered anything and when the focus leaves the textbox, put the defaults back into the textbox.
Also make sure you are not updating the database with the default.