how to clear textboxes using javascript?http://forums.asp.net/t/1200307.aspx/1?how+to+clear+textboxes+using+javascript+Mon, 10 Mar 2008 11:54:26 -040012003072087014http://forums.asp.net/p/1200307/2087014.aspx/1?how+to+clear+textboxes+using+javascript+how to clear textboxes using javascript? <p>hi everyone...</p> <p>Iam new in Client side web</p> <p>so I got this web page that includes 2 asp.net textboxes &amp; a clear button....what I want here is when i click on the button to clear these 2 textboxes on the browser?</p> <p>so how can I do that?</p> <font size="2"> <p>&lt;%</font><font color="#0000ff" size="2">@</font><font size="2"> </font><font color="#800000" size="2">Page</font><font size="2"> </font><font color="#ff0000" size="2">Language</font><font color="#0000ff" size="2">=&quot;C#&quot;</font><font size="2"> </font><font color="#ff0000" size="2">MasterPageFile</font><font color="#0000ff" size="2">=&quot;~/MyMasterPage.master&quot;</font><font size="2"> </font><font color="#ff0000" size="2">AutoEventWireup</font><font color="#0000ff" size="2">=&quot;true&quot;</font><font size="2"> </font><font color="#ff0000" size="2">CodeFile</font><font color="#0000ff" size="2">=&quot;Default3.aspx.cs&quot;</font><font size="2"> </font><font color="#ff0000" size="2">Inherits</font><font color="#0000ff" size="2">=&quot;Default3&quot;</font><font size="2"> </font><font color="#ff0000" size="2">Title</font><font color="#0000ff" size="2">=&quot;Untitled Page&quot;</font><font size="2"> %&gt;</p> </font><font color="#0000ff" size="2"> <p>&lt;</font><font color="#800000" size="2">asp</font><font color="#0000ff" size="2">:</font><font color="#800000" size="2">Content</font><font size="2"> </font><font color="#ff0000" size="2">ID</font><font color="#0000ff" size="2">=&quot;Content1&quot;</font><font size="2"> </font><font color="#ff0000" size="2">ContentPlaceHolderID</font><font color="#0000ff" size="2">=&quot;ContentPlaceHolder1&quot;</font><font size="2"> </font><font color="#ff0000" size="2">Runat</font><font color="#0000ff" size="2">=&quot;Server&quot;&gt;</p> </font><font size="2"> <p>username</p> <p></font><font color="#0000ff" size="2">&lt;</font><font color="#800000" size="2">asp</font><font color="#0000ff" size="2">:</font><font color="#800000" size="2">TextBox</font><font size="2"> </font><font color="#ff0000" size="2">ID</font><font color="#0000ff" size="2">=&quot;TextBox1&quot;</font><font size="2"> </font><font color="#ff0000" size="2">runat</font><font color="#0000ff" size="2">=&quot;server&quot;</font><font size="2"> </font><font color="#ff0000" size="2">Style</font><font color="#0000ff" size="2">=&quot;position: static&quot;&gt;&lt;/</font><font color="#800000" size="2">asp</font><font color="#0000ff" size="2">:</font><font color="#800000" size="2">TextBox</font><font color="#0000ff" size="2">&gt;&lt;</font><font color="#800000" size="2">br</font><font size="2"> </font><font color="#0000ff" size="2">/&gt;</p> </font><font size="2"> <p>password</p> <p></font><font color="#0000ff" size="2">&lt;</font><font color="#800000" size="2">asp</font><font color="#0000ff" size="2">:</font><font color="#800000" size="2">TextBox</font><font size="2"> </font><font color="#ff0000" size="2">ID</font><font color="#0000ff" size="2">=&quot;TextBox2&quot;</font><font size="2"> </font><font color="#ff0000" size="2">runat</font><font color="#0000ff" size="2">=&quot;server&quot;</font><font size="2"> </font><font color="#ff0000" size="2">Style</font><font color="#0000ff" size="2">=&quot;position: static&quot;&gt;&lt;/</font><font color="#800000" size="2">asp</font><font color="#0000ff" size="2">:</font><font color="#800000" size="2">TextBox</font><font color="#0000ff" size="2">&gt;&lt;</font><font color="#800000" size="2">br</font><font size="2"> </font><font color="#0000ff" size="2">/&gt;</p> </font><font size="2"></font><font color="#0000ff" size="2">&lt;</font><font color="#800000" size="2">asp</font><font color="#0000ff" size="2">:</font><font color="#800000" size="2">Button</font><font size="2"> </font><font color="#ff0000" size="2">ID</font><font color="#0000ff" size="2">=&quot;ClearButton&quot;</font><font size="2"> </font><font color="#ff0000" size="2">runat</font><font color="#0000ff" size="2">=&quot;server&quot;</font><font size="2"> </font><font color="#ff0000" size="2">Style</font><font color="#0000ff" size="2">=&quot;position: static&quot;</font><font size="2"> </font><font color="#ff0000" size="2">Text</font><font color="#0000ff" size="2">=&quot;Button&quot;</font><font size="2"> </font><font color="#0000ff" size="2">/&gt;</font><font color="#0000ff" size="2"> <p>&lt;/</font><font color="#800000" size="2">asp</font><font color="#0000ff" size="2">:</font><font color="#800000" size="2">Content</font><font color="#0000ff" size="2">&gt;</font></p> <p><font color="#0000ff" size="2"></font>&nbsp;</p> <p><font color="#0000ff" size="2">thanks alot</p> </font> 2008-01-03T00:26:20-05:002087096http://forums.asp.net/p/1200307/2087096.aspx/1?Re+how+to+clear+textboxes+using+javascript+Re: how to clear textboxes using javascript? <p>&nbsp;&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;<br> &lt;head runat=&quot;server&quot;&gt;<br> &nbsp;&nbsp;&nbsp; &lt;title&gt;Untitled Page&lt;/title&gt;<br> &nbsp;&nbsp;&nbsp; &lt;script type=&quot;text/javascript&quot; language=&quot;javascript&quot;&gt;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; function ClearTextboxes()<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; document.getElementById('TextBox1').value = '';<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; document.getElementById('TextBox2').value = '';&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br> &nbsp;&nbsp;&nbsp; &lt;/script&gt;<br> &lt;/head&gt;<br> &lt;body&gt;<br> &nbsp;&nbsp;&nbsp; &lt;form id=&quot;form1&quot; runat=&quot;server&quot;&gt;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;div&gt;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; username<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;asp:TextBox ID=&quot;TextBox1&quot; runat=&quot;server&quot;&gt;&lt;/asp:TextBox&gt;&lt;br /&gt;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; password<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;asp:TextBox ID=&quot;TextBox2&quot; runat=&quot;server&quot;&gt;&lt;/asp:TextBox&gt;&lt;br /&gt;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;asp:Button ID=&quot;ClearButton&quot; runat=&quot;server&quot; Text=&quot;Button&quot; CausesValidation=&quot;false&quot; OnClientClick=&quot;ClearTextboxes();&quot; /&gt;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/div&gt;<br> &nbsp;&nbsp;&nbsp; &lt;/form&gt;<br> &lt;/body&gt;<br> &lt;/html&gt;</p> 2008-01-03T02:02:04-05:002087097http://forums.asp.net/p/1200307/2087097.aspx/1?Re+how+to+clear+textboxes+using+javascript+Re: how to clear textboxes using javascript? <p>Well you can add a reset html button....</p> <p>or call document.forms[0].reset();</p> <p>or</p> <p>function clear(){<br> &nbsp; document.getElementById(&quot;&lt;%= <font color="#0000ff" size="2">TextBox1.ClientID </font> %&gt;&quot;).value = &quot;&quot;;<br> &nbsp; document.getElementById(&quot;&lt;%= <font color="#0000ff" size="2">TextBox2.ClientID </font> %&gt;&quot;).value = &quot;&quot;;<br> }<br> </p> <p>Eric&nbsp;</p> <p>&nbsp;</p> 2008-01-03T02:02:07-05:002087238http://forums.asp.net/p/1200307/2087238.aspx/1?Re+how+to+clear+textboxes+using+javascript+Re: how to clear textboxes using javascript? function clrCtrl() { document.getElementById('ctl00_ContentPlaceHolder1_TextBox1').value = &quot;&quot;; document.getElementById('ctl00_ContentPlaceHolder1_TextBox2').value = &quot;&quot;; } 2008-01-03T03:54:25-05:002087247http://forums.asp.net/p/1200307/2087247.aspx/1?Re+how+to+clear+textboxes+using+javascript+Re: how to clear textboxes using javascript? <p>&nbsp;<pre class="prettyprint">function clrCtrl() { document.getElementById('ctl00_ContentPlaceHolder1_TextBox1').value = &quot;&quot;; document.getElementById('ctl00_ContentPlaceHolder1_TextBox2').value = &quot;&quot;; } &lt;asp:Button ID=&quot;ClearButton&quot; runat=&quot;server&quot; Style=&quot;position: static&quot; Text=&quot;Button&quot; OnClientClick=&quot;clrCtrl()&quot; /&gt;</pre>&nbsp;</p> 2008-01-03T04:01:48-05:002090226http://forums.asp.net/p/1200307/2090226.aspx/1?Re+how+to+clear+textboxes+using+javascript+Re: how to clear textboxes using javascript? <p>thanks all for replying</p> <p>well it worked fine with me...</p> <p>but I got a question: I notice when I click on the button it goes to the server...I can see the Green loading bar down</p> <p>so Can't I do this without going to the server?</p> <p>&nbsp;</p> 2008-01-04T10:55:12-05:002090314http://forums.asp.net/p/1200307/2090314.aspx/1?Re+how+to+clear+textboxes+using+javascript+Re: how to clear textboxes using javascript? <p>Use a regular HTML button instead of an ASP:Button:<br> &nbsp;&lt;input type=&quot;button&quot; value=&quot;Clear&quot; onclick=&quot;JavaScript: clearTextBoxes();&quot;&gt;</p> <p>Or return false in the OnClientClick event&nbsp;of the ASP:Button:<br> &nbsp;&nbsp;&nbsp;&nbsp; &lt;asp:Button ... OnClientClick=&quot;clearTextBoxes(); return false;&quot; /&gt;</p> <p>NC...<br> </p> 2008-01-04T11:53:44-05:002097980http://forums.asp.net/p/1200307/2097980.aspx/1?Re+how+to+clear+textboxes+using+javascript+Re: how to clear textboxes using javascript? <p>&nbsp;</p> thanks alot it worked fine with me 2008-01-08T23:33:05-05:002216303http://forums.asp.net/p/1200307/2216303.aspx/1?Re+how+to+clear+textboxes+using+javascript+Re: how to clear textboxes using javascript? <p></p> <blockquote><span class="icon-blockquote"></span> <h4>NC01</h4> <p></p> <p>Use a regular HTML button instead of an ASP:Button:<br> &nbsp;&lt;input type=&quot;button&quot; value=&quot;Clear&quot; onclick=&quot;JavaScript: clearTextBoxes();&quot;&gt;</p> <p>Or return false in the OnClientClick event&nbsp;of the ASP:Button:<br> &nbsp;&nbsp;&nbsp;&nbsp; &lt;asp:Button ... OnClientClick=&quot;clearTextBoxes(); return false;&quot; /&gt;</p> <p>NC...<br> </p> </blockquote> &nbsp; <p>&nbsp;</p> <p>Well, I try to use your code , but nothing happend </p> <p>it gives me error in page</p> <p>I belive your code is right, but I don't know what is wrong with my page</p> <p>here is my code</p> <p>&nbsp;</p> <pre class="prettyprint"></pre><pre class="prettyprint"> &lt;script type='text/javascript' language='javascript'&gt;<br></pre><pre class="prettyprint">function ClearTextboxes() <br>{<br>try<br> {<br> document.getElementById('ctl00_BodyContentPlaceHolder_txtSchoolNameEN').value = '';<br> document.getElementById('ctl00_BodyContentPlaceHolder_txtSchoolNameAR').value = '';<br> document.getElementById('ctl00_BodyContentPlaceHolder_drpGovernmentalDept').selectedIndex = 0;<br> document.getElementById('ctl00_BodyContentPlaceHolder_txtNotes').value = '';<br> }<br> catch(err)<br> {var txt='Errot=&gt;'+err.description;/*alert(txt);*/}<br> }<br> &lt;/<span class="tag">script</span>&gt;</pre><p></p><p>and I call the function like that</p><p></p><pre class="prettyprint"> XX &lt;<span class="tag">IMG</span><span class="attr"> style=</span><span class="attrv">"CURSOR: hand"</span><span class="attr"> id=</span><span class="attrv">"ClearForm"</span><span class="attr"> onclick=</span><span class="attrv">"javascript:clearTextBoxes();"</span><br><span class="attr"> src=</span><span class="attrv">"../images/btncancel.gif"</span> /&gt;</pre> <p>&nbsp;&nbsp;</p> <p>but I got that error</p> <p>Object Expected&nbsp; and it refers to the line XX&nbsp;</p> <p>&nbsp;any help please ????????<br> </p> 2008-03-06T12:15:03-05:002216376http://forums.asp.net/p/1200307/2216376.aspx/1?Re+how+to+clear+textboxes+using+javascript+Re: how to clear textboxes using javascript? <p>Because your function is named <strong>ClearTextboxes()</strong> and you are calling <strong>clearTextBoxes()</strong>. <strong>Remember that JavaScript is case-sensitive</strong>. Try</p> <p>&lt;IMG style=&quot;CURSOR: hand&quot; id=&quot;ClearForm&quot; onclick=&quot;javascript:<strong>C</strong>learTextBoxes();&quot; src=&quot;../images/btncancel.gif&quot; /&gt;</p> <p>or change the function name to <br> function <strong>c</strong>learTextBoxes()<br> {<br> &nbsp;&nbsp; ...</p> <p>NC...<br> </p> 2008-03-06T12:45:40-05:002221278http://forums.asp.net/p/1200307/2221278.aspx/1?Re+how+to+clear+textboxes+using+javascript+Re: how to clear textboxes using javascript? <p>Well, it's great, sorry for late reply, I lost my account password !!!! thank you anyway&nbsp;</p> 2008-03-09T11:58:49-04:002222666http://forums.asp.net/p/1200307/2222666.aspx/1?Re+how+to+clear+textboxes+using+javascript+Re: how to clear textboxes using javascript? <p>So I take it that fixed your problem?</p> <p>NC...</p> <p>&nbsp;</p> 2008-03-10T11:54:26-04:00