Tuesday, 3 May 2016

Getting Textbox value in Javascript

I am trying to get the value of the textbox in a javascript, but its not working. Given below is the code of my test page

<%@ Page Title="" Language="VB" MasterPageFile="~/Site.Master" AutoEventWireup="false"
CodeFile="test3.aspx.vb" Inherits="test3" %>

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="Server">
<script language="javascript">

    function GetAlert() {

        var TestVar = document.getElementById('txt_model_code').value;
        alert(TestVar);

    }

</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="Server">
<asp:TextBox ID="txt_model_code" runat="server" ></asp:TextBox><br /><br />

<input type="button" value="db Function" onclick="GetAlert()" /><br />
</asp:Content>
So where am i going wrong?? How to get the text entered in the textbox in the javascript???

share improve this question
Asked
Feb 18 '11 at 9:22

Shijilal
819●6●20●39
6  
do this: var TestVar = document.getElementById('<%= txt_model_code.ClientID %>').value; – deostroll Feb 18 '11 at 9:25
   
@deostroll thanks it worked.. Now what to do if i want to show that value in another textbox – Shijilal Feb 18 '11 at 9:42
2  
document.getElementById('<%= anothertextboxid.ClientID %>').value=TestVar – Nirmal Feb 18 '11 at 9:48
   
as it is asp.net control, you can not get the value using getElementById(). because asp.net controls are converted to html controls and "id" changes on the browser. for more details refer: coding-issues.blogspot.in/2013/10/… – Ranadheer Reddy Oct 31 '13 at 9:36
add a comment
6 Answers order by 
up vote
11
down vote
accepted
Use

document.getElementById('<%= txt_model_code.ClientID %>')
instead of

document.getElementById('txt_model_code')`
Also you can use onClientClick instead of onClick.

share improve this answer
Answered
Feb 18 '11 at 9:38

Nirmal
965●7●25 Edited
Jun 7 '13 at 15:20

David L
13.5k●3●18●43
   
when i use the function in text_model_code onTextChange, it's saying the function is not part of my aspx file.. Why??? <asp:TextBox ID="txt_model_code" runat="server" OnTextChanged="GetModelPageMethod()"> – Shijilal Feb 18 '11 at 10:03
1  
I think you need to use this in onChange="GetModelPageMethod();" – Nirmal Feb 18 '11 at 10:07
   
ohk thanks..it worked – Shijilal Feb 18 '11 at 10:15
add a comment
up vote
4
down vote
This is because ASP.NET it changing the Id of your textbox, if you run your page, and do a view source, you will see the text box id is something like

ctl00_ContentColumn_txt_model_code

There are a few ways round this:

Use the actual control name:

var TestVar = document.getElementById('ctl00_ContentColumn_txt_model_code').value;

use the ClientID property within ASP script tags

document.getElementById('<%= txt_model_code.ClientID %>').value;

Or if you are running .NET 4 you can use the new ClientIdMode property, see this link for more details.

http://weblogs.asp.net/scottgu/archive/2010/03/30/cleaner-html-markup-with-asp-net-4-web-forms-client-ids-vs-2010-and-net-4-0-series.aspx1

No comments:

Post a Comment