Javascript from aspx

kcwallace

Centurion
Joined
May 27, 2004
Messages
175
Location
Austin, TX
I need to write code that will, for example, clear a textbox but not reload the page. It is my understanding that you need to use Javascript for this. If so, how?
 
I'd reccomend scanning through some info about Javascript, but the property you're looking for can be found here.

This will go inside your <head> tag. This simply makes a function ClearTextBox() that you can call from any <script> block following it.

Code:
<script type="text/javascript">
function ClearTextbox() {
  myTextBox.value = ""; // Clear the textbox, where myTextBox is the name
}
</script>

And here is an example of the textbox control you'd use, and the button which would clear it.
Code:
<input type="text" name="myTextBox" />
<input type="button" value="Clear" onClick="javascript:ClearTextBox()" />

This is untested code, but you should not have any issues. It is pretty straightforward. Just remember to replace myTextBox with the actual name of the input field.
 
or simply:
Code:
<input type="text" name="myTextBox" />
<input type="button" value="Clear" 
	onClick="javascript:document.all.myTextBox.value=''" />
 
As far as JavaScript is concerned, after the ASP.NET server renders the web page, the controls actually become standard HTML input controls. Since JavaScript is run on the client side, and the only thing the client receives are HTML controls, this code will work.
 
Back
Top