ASP.NET TextBox OnFocus

benamaster

[H]ard|Gawd
Joined
Aug 16, 2005
Messages
1,146
I have an ASP.NET 2.0 form with a TextBox for searching, I want it to have the default text "Search" but when a user clicks the TextBox the default text goes away and its blank for them to type in what they want to search for and then click the button to search for it. I know how to do it with a postback, but I need it to get rid of the default text without a postback.

Any Ideas?

Thanks in Advance! :)
 
Havn't tried it with 2.0 yet but I think the same idea holds. In your code behind, add this to your text box. textbox.addAttribute("onBlur", "javascript_name")
And in your aspx, have a javascript function the same as javascript_name.

In the function, you can test the value of the text box by using the getElementById('textbox id').value If that value == 'Search', then value = ''

Hope this helps!
 
you'll want something like this.

Code:
<html>
<head>
<title>search box test</title>
<script type="text/javascript">

function clearSearch()
{
	document.getElementById('searchbox').value = '';
}

function setSearch()
{
document.getElementById('searchbox').value = 'search';
}

</script>
</head>
<body onload="setSearch()">
<input type="text" id="searchbox" onblur="setSearch()" 

onclick="clearSearch()"/>
</body>
</html>
 
I figured out the ASP.NET way, this works in ASP.NET 2.0 I am not sure about 1.1

searchTextBox.Attributes.Add("onFocus","this.value='';");

That makes a TextBox that has text in it go blank on click. Simple enough Eh?

Thanks to Mercypoint for guiding me to the Arrtibutes and to generelz for the example.
 
Back
Top