?
Solved

Problem in OnKeyDown

Posted on 2011-03-24
8
Medium Priority
?
279 Views
Last Modified: 2012-06-21
Hi

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 %>");        
alert(txtMultilineTextBox.value.length);

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.


Regards
Karan
0
Comment
Question by:KaranGupta
[X]
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
8 Comments
 
LVL 19

Expert Comment

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

~ Mrugesh
0
 
LVL 20

Expert Comment

by:Proculopsis
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.


0
 

Author Comment

by:KaranGupta
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.

Proculopsis:

What is the solution for that
0
RHCE - Red Hat OpenStack Prep Course

This course will provide in-depth training so that students who currently hold the EX200 & EX210 certifications can sit for the EX310 exam. Students will learn how to deploy & manage a full Red Hat environment with Ceph block storage, & integrate Ceph into other OpenStack service

 
LVL 40

Expert Comment

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

0
 
LVL 20

Accepted Solution

by:
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">
<html>
<head>
<title>http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_26908183.html</title>
<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 );
  });
 
});

</script>
</head>
<body>

<input id="text" />

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

</body>
</html>

Open in new window

0
 
LVL 29

Expert Comment

by:Badotz
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.
0
 
LVL 19

Assisted Solution

by:Rikin Shah
Rikin Shah earned 1000 total points
ID: 35205248
I hope this is what you're looking for...
http://www.smartwebby.com/dhtml/textbox_characters_counter.asp
0
 
LVL 3

Expert Comment

by:timhigham
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.

0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

770 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