Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Problem in OnKeyDown

Posted on 2011-03-24
Medium Priority
Last Modified: 2012-06-21

I have a textbox

<asp:TextBox ID = "txtMultilineTextBox" runat = "server"></asp:TextBox>

On Key down event I have written following code.

var txtMultilineTextBox = document.getElementById("<%=txtMultilineTextBox.ClientID %>");        

When II press space on the textbox for the first time I am getting alert with value 0 when I press space again I got 1. Why is the first time I am getting the length of textbox as zero.

Question by:KaranGupta
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 19

Expert Comment

by:Rikin Shah
ID: 35204942
Change the event to OnKeyPress/OnKeyUp and check

~ Mrugesh
LVL 20

Expert Comment

ID: 35205009

>>>I am getting alert with value 0 when I press space again I got 1. Why is the first time I am getting the length of textbox as zero.

It's because in this event handler the effect of the key press has not yet been applied to the input field.


Author Comment

ID: 35205153
Hi rikin_shah:

I can't use on key up. The main reason is I am implementing character count thing in JavaScript so if I move my code to onkeyup and press the key without pressing up then it will not count character.


What is the solution for that
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 35205231
use onkeypress event. It will be triggered after the text is entered in the textbox.

LVL 20

Accepted Solution

Proculopsis earned 1000 total points
ID: 35205242

Try something like this if you want a count/limit:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">

jQuery(document).ready( function() {

  $("#text").keyup( function() {
    var maximumLength = 9;
    if ( $(this).val().length > maximumLength ) $(this).val( $(this).val().substring( 0, maximumLength ) );
    $("#count").text( $(this).val().length );


<input id="text" />

Count: <span id="count">0</span>/9


Open in new window

LVL 29

Expert Comment

ID: 35205247
>>if I move my code to onkeyup and press the key without pressing up then it will not count character.

onkeyup is the event that fires when you release a key. It does not mean the event that fires when you press the up (arrow) key.
LVL 19

Assisted Solution

by:Rikin Shah
Rikin Shah earned 1000 total points
ID: 35205248
I hope this is what you're looking for...

Expert Comment

ID: 35219748
for each element have and invisible notification div EG

<div id="inputXerror">ERROR</div>

use javascript to check the whole form for 'onsubmit', making error div visible and returning false if there is an unacceptable input to prevent submission.


Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show, step by step, how to integrate R code into a R Sweave document
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question