Click here to Skip to main content
15,903,385 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to make a counter for each "keypress".

All it shows is the text in TextBox1 but nothing in TextBox3.

Any suggestions?

ASP.NET
<script src="../Scripts/jquery-1.4.1.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            var minCount = 1;
            var maxCount = 500;
            $("#TextBox1").keypress(function () {
                var strCount = $("#TextBox1").val().length;
                $("#TextBox3").val(strCount);
            });
        });
    </script>
<asp:TextBox ID="TextBox1" TextMode="MultiLine" Rows="25" Width="800" Height="100" runat="server"></asp:TextBox><br />
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
Posted
Comments
nika2008 1-Mar-13 19:36pm    
can you use javascript or only jquery?

For the "run at server" tags the attribute ID has a different connotation than usual in HTML. ID is the name of the variable used in the code behind. If you want to get the ID of the tag as present on the client side do this:

ASP.NET
<script src="../Scripts/jquery-1.4.1.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            var minCount = 1;
            var maxCount = 500;
            $("<%= TextBox1.ClientID %>").keypress(function () {
                var strCount = $("<%= TextBox1.ClientID %>").val().length;
                $("<%= TextBox3.ClientID %>").val(strCount);
            });
        });
    </script>
<asp:TextBox ID="TextBox1" TextMode="MultiLine" Rows="25" Width="800" Height="100" runat="server"></asp:TextBox><br />
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>


Regards,

— Manfred
 
Share this answer
 
v2
Comments
Teledextri 1-Mar-13 11:10am    
Way to go with the $("<%= TextBox1.ClientID %>") added it to my program.

The only problem it still did not fix the keypress issue.

Maybe textbox keypress are not covered by jquery-1.4.1.js?
The real solution is:

$("#<%= TextBox1.ClientID %>")

do not forget the # in front.

With that it works correctly.
 
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