Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
This is my ascx code:
XML
<script language="javascript" type="text/javascript">
           function fnMsg() {
               var value = document.getElementById('txt').value

           }
       </script>


HTML
<input id="txt" style="WIDTH: 12%"  type="text" maxlength="10" name="txt"  runat="server"/>
  <input type="button"  runat="server" value="click"  önclick="fnMsg();" style="width: 33px" />


I couldnt get the value of text box in javascript.

The error is document.getElementById('txt') is undefined.

Is there anyway to access the value
Posted
Updated 14-Nov-12 20:50pm
v3
Comments
Anshu BM 15-Nov-12 2:48am    
Is there any id as "txtAdmitFrom" in your code
Manfred Rudolf Bihy 15-Nov-12 2:51am    
OP changed his code inconsistently, I fixed it.

If you are using asp.net 4 you can use
ASP.NET
<asp:Textbox ClientIdMode="Static">

Instead of your server side html tag.
Or obtain client id of this tag in aspx/ascx
HTML
var id="<%=txt.ClientID %>";
 
Share this answer
 
v2
Comments
Divya RS 15-Nov-12 3:58am    
thank u both n.podbielski and Manfred R.Bihy both of the solutions worked out for me. . .
n.podbielski 15-Nov-12 4:04am    
No problem. Glad I could help.
Manfred Rudolf Bihy 15-Nov-12 4:16am    
Glad it helped!
The ID attribute of a HTML tag that is modified with the runat="Server" attribute changes. This ID is only available on the server side now and to get the ID of that tag as it is rendered in the HTML sent to the browser you'll have to user <%= txt.ClientID %>

See here:

XML
<script language="javascript" type="text/javascript">
    function fnMsg() {
        var value = document.getElementById('<%= txt.ClientID %>').value

    }
</script>


Regards,

— Manfred
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900