2

I want to clear an asp textbox when click to a button (html).

so basically the JS function is:

<script type="text/javascript">
    function clearTextBox() {
        document.getElementByID('<%=txtFName.ClientID%>').value = "";
    }
</script>

And an asp.net TextBox

<asp:TextBox ID="txtFName" runat="server" class="form-control" placeholder="First name" AutoCompleteType="Disabled" MaxLength="30" />

And a HTML button

<span class="input-group-btn">
<button class="btn default" id="clearButton" type="button" onclick="clearTextBox()"><i class="fa fa-times"></i></button>
</span>

All buttons and other controls are in an asp update-panel (don't know if it makes different)

Nothing happend when I clicked to the HTML clearButton, error on the web console:

Uncaught TypeError: undefined is not a function
clearTextBox 
onclick
0

6 Answers 6

2

Try to pass Id of textbox, in your case it should be:

document.getElementByID('txtFName').value = "";

and then assign method to you input something like this

asp:textbox ..... onClick="yourMethodName"
Sign up to request clarification or add additional context in comments.

2 Comments

tried document.getElementByID('txtFName').value = ""; but same error
i have one more idea. Try to change method inside you textbox OnClick to OnClientClick=("YourJavaScriptMethodName")
2

Move your following js code in page header or some other place, right now it is unable to find your js function. It must be breaking due to some error or break. Or try to put js alert/debugger in your js function to make sure that are you even able to find this function. Clearing the textbox is next step.

<script type="text/javascript">
function clearTextBox() {
    alert('Hello World');
}</script>

Now, if you are able to see your alert then your code can find your function. Then modify your textbox tag like:

<asp:TextBox CliendIDMode="Static" ID="txtFName" runat="server" class="form-control" placeholder="First name" AutoCompleteType="Disabled" MaxLength="30" />

Now, modify your clearTextBox function like:

<script type="text/javascript">
function clearTextBox() {
    document.getElementById('txtFName').value = '';
}</script>

5 Comments

the alert worked but when change to document.getElementById('txtFName').value = ''; it showed same error
open the browser console and type $('input'), now check can you see your textbox 'txtFName' in this list, try to find its exact client-side id
yes I can see it, it looks as: <input name="txtFName" type="text" autocomplete="off" maxlength="30" id="txtFName" cliendidmode="Static" class="form-control" placeholder="First name">
so type document.getElementById('txtFName').value = '' in console, if it is working in console, it should be in JS as well
yes typed document.getElementById('txtFName').value = 1000 and it workes, textbox showed 1000, but still same error when I clicked to the button
1

I will suggest add JS into Head part of page. then it will not create the Uncaught ReferenceError: clearTextBox is not defined

2 Comments

I put it on <head> but still same problem, in fact if I put other JS and Jquery on the head it will cause a lot of problem unless I move them under body tag
I click "View page source" and see all, JS and all control codes.
0

You need to add

ClientIDMode="Static"

so you text box will look like this

<asp:TextBox CliendIDMode="Static" ID="txtFName" runat="server" class="form-control" placeholder="First name" AutoCompleteType="Disabled" MaxLength="30" />

In asp.net when page renders the id of controls changes

I suggest you should use jquery.

$("#txtFName").val("");

Also return false in your javascript function

Also try in one block your JS

8 Comments

can you post full jquery function and how to call it on the control, my JS and JQuery is weak
@RonaldinhoState If you are using VS 2010. Jquery is included in website project. Just drag 3 jquery files from script folder and drop it on your page's header section and use the line of code I wrote above
@RonaldinhoState Updated my answer should return false on your javascript function
I think I have Jquery references included, I still having hard time to figure out how to follow your answer, have no idea about how to use JS and Jquery.
@RonaldinhoState post ur html in question I think u r writng ur JS code not correctly
|
0
function clearTextBox() {
   var elements = [] ;
    elements = document.getElementsByClassName("form-control"); // your class name 

    for(var i=0; i<elements.length ; i++){
       elements[i].value = "" ;
    }
}

1 Comment

I have more than one control using form-control CSS class, how do I do?
0

Change

document.getElementByID

to

document.getElementById

and it worked. Credit goes to Mudassar

Thanks everybody!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.