user1758415 user1758415 - 8 months ago 30
Javascript Question

How to change the value of an asp:textbox from javascript

I have an asp:textbox

<asp:textbox ID="NewName" runat="server" />

and when a user clicks the "Cancel" button, I want to erase whatever is in that text box by making the value just be "". If it were a an input field of type text, I could just do this in javascript:

document.getElementById('NewName').value= "";

But I can't just do this because it is an asp:textbox.


When your asp:textbox is rendered to the UI, then yes, it is just an input field of type text. The only part that may be an issue is the ID, but that can be set using ClientIDMode:

<asp:textbox ID="NewName" runat="server" ClientIDMode="Static" />

renders to:

<input type="text" id="NewName" />

and thus the JavaScript you use should work perfectly.