[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 10287
  • Last Modified:

clear textbox

in ASP.NET 2.0 I have a page with a textbox and a button.
I want that push on the button will clear the textbox in clientside in javascript.
How do I do it?
ThankYou
Anat
0
ANAT2403
Asked:
ANAT2403
  • 5
  • 4
  • 2
1 Solution
 
basicinstinctCommented:
Here's a nice little sample page for you:

<html>
<head>
<title>New Page 1</title>

<script language="JavaScript">
    function clearText(boxId)
    {
      txtBox = document.getElementById(boxId);
      txtBox.value = "";
    }
</script>

</head>

<body>
  <textarea id="ta">This is a whole bunch of text.</textarea>
  <input value="Clear TextArea" type="button" onclick="clearText('ta');">
  <br/><br/>
  <input id="tb" type="text" value="This is a whole bunch of other text." >
  <input value="Clear TextBox" type="button" onclick="clearText('tb');">
</body>

</html>
0
 
basicinstinctCommented:
By the way, with the code above, just copy it and paste it into an empty text file, save it as 'text.html' and open it in your browser.

And here it is running on my webserver:

http://malydog.homeip.net/ee/Q_21853592.html
0
 
basicinstinctCommented:
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
Michel PlungjanIT ExpertCommented:
@BI: If you want this to work in all browsers, wrap in a form and pass the element:


<form>
  <textarea NAME="ta">This is a whole bunch of text.</textarea>
  <input value="Clear TextArea" type="button" onClick="this.form.ta.value=''">
</form>
0
 
basicinstinctCommented:
Oh, mplungian, I'd appreciate if you could explain that briefly... Are you saying that not all browsers support "document.getElementById()"?  
0
 
Michel PlungjanIT ExpertCommented:
Yes. When handling forms I never use document.getElementById since
1. the elements need a name anyway to pass the values to the server
2. only IE5+ and NS6+ (and FF and mozilla and opera) understands it
IE4, NS4, Older opera, my opera mini on my phone and so on do not understand it but they all understand form element access

Michel
0
 
basicinstinctCommented:
Ok - thanks
0
 
ANAT2403Author Commented:
Hi ,
Well I forgot to mention that I have in my page a formview and in it I have a textbox.
So how can I turn to the text box while it is in a formview? In server side I use the findcontrol of the formview
What do I do in the Javascript?
Thank You
Anat
0
 
Michel PlungjanIT ExpertCommented:
What is a form view?
0
 
ANAT2403Author Commented:
a formview is a control in ASP.NET 2.0
0
 
Michel PlungjanIT ExpertCommented:
I guess you need to do
btn.OnClientClick="this.form.ta.value=''"
I have no experience with asp.net
Search google for onClientClick

I found this too:
When using onclientclick on a <asp:button>, you have to set UseSubmitBehavior to false to allow you to cancel the postback. This isn't documented (or intuitive)
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 5
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now