Click here to Skip to main content
15,885,212 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my Asp.Net project, I tried to use JavaScript to catch the text change. the related code are below:

XML
<script type="text/javascript">
     function txtMsg_TextChanged(txt) {
         //alert("Hi!");
         var s = txtMessage.toString();
     }
</script>

XML
<asp:TextBox ID="txtMessage"
                runat="server"
                Columns="40"
                Rows="6" AutoPostBack="True"
                TextMode="MultiLine" ontextchanged="txtMessage_TextChanged"
                onchange="txtMsg_TextChanged(this)" >
</asp:TextBox>

In Code behind, Page_Load()
C#
if (!Page.IsPostBack) {
    txtMessage.Attributes.Add("onchange", "txtMsg_TextChanged();
}"

When I debugged the code on the function txtMsg_TextChanged(txt), I got a warning. Then I checked both variables: txt, and txtMessage - both of them have no value. How can get the value of txtMessage that I typed in the textbox? Thanks.
Posted

Add this script in the head section:
JavaScript
<script type="text/javascript">
function ChangeText(obj) {
    alert(obj.value);
}
</script>

This is the markup for the textbox:
ASP.NET
<asp:textbox id="myTextBox" runat="server" autopostback="true" ontextchanged="myTextBox_TextChanged"></asp:textbox>

And this is the code behind:
C#
if (!IsPostBack)
{
    myTextBox.Attributes.Add("onchange", "ChangeText(this);");
}
protected void myTextBox_TextChanged(object sender, EventArgs e)
{
    Response.Write(myTextBox.Text);
}
 
Share this answer
 
Hi,
As you want to catch text change event on javascript side, you have two options.
1. Use onchange event. This event will fire javascript function as you loose focus from textbox.
2. Use keyup event, it will fire javascript function on each and every key up event.

Note:- As you don't want to fire server side event, do not use AutoPostBack="true" in textbox.
ASP.NET
<!-- Your Markup-->
<asp:textbox id="txtMessage" runat="server" columns="40" rows="6" textmode="MultiLine" onchange="txtMsg_TextChanged(this)" ></asp:textbox>
<asp:textbox id="txtMessage1" runat="server" columns="40" rows="6" textmode="MultiLine" onkeyup="txtMsg_TextChanged1(this)" ></asp:textbox>

<script type="text/javascript">
     // this function is called as you loose focus from textbox "txtMessage"
     function txtMsg_TextChanged(a) {
         alert(a.value);
     }

     // this function is called on every keyup event of textbox "txtMessage1"
     function txtMsg_TextChanged1(a) {
         alert(a.value);
     }
</script>

Try both, use as per your need.
Hope it helps you.
Thanks.
 
Share this answer
 
v2
Use Jquery Get Element By ID or It can also be done with bare javascript.

C#
function txtMsg_TextChanged(txt) {
        //alert("Hi!");
        var s = $(txt).val()
    }



http://www.w3schools.com/jquery/sel_id.asp[^]
 
Share this answer
 
v5
If you want to call a javascript function on Textbox textchange event.
Than you can use the following in design side.


C#
<asp:textbox id="txt1" runat="server" ontextchanged="javascript:Jfunction(this);" xmlns:asp="#unknown"></asp:textbox>



and in the head in script tag.

use..

JavaScript
function Jfunction(txtbx)

{


}


Hope it will help. :)
 
Share this answer
 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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