views:

578

answers:

4

Generally i saw any registration form the textboxes are filled with for example Enter first name or Enter Last name here like that how can i write like that. Is it javascript? can u tell me how can i write that like that help me thanks

A: 

You have to set the value property of the textbox to the desired text like

<input type="text" id="txt1" value="Enter text here" />

When input receives focus and is the value is the default one then clear the value. And if the user hasn't entered anything and when focus leaves the textbox put the default values back to the textbox.

Also make sure that you don't update database with the default value.

rahul
no no u not understand me when i place the cursor on that particular textbox the text should dissapear and i can write what i want
Surya sasidhar
to do that, you'll need to add onclick="this.value=''".
jrummell
That will erase your text every time you click on it. You should erase the text on the onfocus event of the input, but only if it matches your initial text.
dpb
@dpb - good call!
jrummell
+1  A: 

As you suggested yourself, you could use JavaScript called on the onload event, something like:

<body onload="document.getElementById('firstName').value = 'Enter first name'" ...>

You could group multiple instructions in a function and just call that on onload.

Or you could just set the value directly in the input:

<input type="text" id="firstName" name="firstName" value="Enter first name" />
dpb
+3  A: 

What you want is a TextBoxWatermark. The AJAX Control Toolkit has one.

Also, here is an example of a custom ASP.NET Textbox Watermark.

EDIT: Example below is from link referenced 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 Behind:

string strWatermarkSearch = "Search";
txtSimpleSearch.Text = strWatermarkSearch;
txtSimpleSearch.Attributes.Add("onfocus", "WatermarkFocus(this, '" + strWatermarkSearch + "');");
txtSimpleSearch.Attributes.Add("onblur", "WatermarkBlur(this, '" + strWatermarkSearch + "');");
rick schott
exactly mr.rick_schott that is what i want?
Surya sasidhar
i mean that exactly the watermark effect i need
Surya sasidhar
+1  A: 

Based on your comments to the post by phoenix you could 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>
Andy Rose