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 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!
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  

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

  Print Answers RSS
0 OriginalGriff 380
1 Gihan Liyanage 198
2 Sergey Alexandrovich Kryukov 150
3 ChauhanAjay 120
4 BotCar 105
0 Sergey Alexandrovich Kryukov 9,056
1 OriginalGriff 7,981
2 CPallini 2,613
3 Richard MacCutchan 2,121
4 Abhinav S 1,928


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

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100