Problem in OnKeyDown

Posted on 2011-03-24
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
Independent Software Vendors: 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!

LVL 40

Expert Comment

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

LVL 20

Accepted Solution

Proculopsis earned 250 total points
ID: 35205242

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "">
<script src="" 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 250 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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

726 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