Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
This is my ascx code:
<script language="javascript" type="text/javascript">
           function fnMsg() {
               var value = document.getElementById('txt').value
 
           }
       </script>
 
<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 14-Nov-12 20:40pm
Edited 14-Nov-12 20:50pm
v3
Comments
y5me211112 at 15-Nov-12 2:48am
   
Is there any id as "txtAdmitFrom" in your code
Manfred R. Bihy at 15-Nov-12 2:51am
   
OP changed his code inconsistently, I fixed it.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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:
 
       <script language="javascript" type="text/javascript">
           function fnMsg() {
               var value = document.getElementById('<%= txt.ClientID %>').value
 
           }
       </script>
 
Regards,
 
— Manfred
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

If you are using asp.net 4 you can use
<asp:Textbox ClientIdMode="Static">
Instead of your server side html tag.
Or obtain client id of this tag in aspx/ascx
var id="<%=txt.ClientID %>";
  Permalink  
v2
Comments
Anusha SR at 15-Nov-12 3:58am
   
thank u both n.podbielski and Manfred R.Bihy both of the solutions worked out for me. . .
n.podbielski at 15-Nov-12 4:04am
   
No problem. Glad I could help.
Manfred R. Bihy at 15-Nov-12 4:16am
   
Glad it helped!

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Shai Vashdi 1,858
1 Tadit Dash 330
2 OriginalGriff 278
3 Sergey Alexandrovich Kryukov 275
4 Manas Bhardwaj 249
0 Sergey Alexandrovich Kryukov 9,530
1 OriginalGriff 5,711
2 Peter Leow 4,320
3 Maciej Los 3,540
4 Abhinav S 3,373


Advertise | Privacy | Mobile
Web04 | 2.8.140415.2 | Last Updated 15 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid