character count as I am typing into an input textbox

Posted on 2010-08-12
Last Modified: 2012-05-10
character count as I am typing into an input textbox
Question by:rgb192
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 40

Expert Comment

ID: 33422458
LVL 23

Expert Comment

by:Kamaraj Subramanian
ID: 33422459
LVL 29

Expert Comment

ID: 33423815
Something along these lines (untested, sorry):

<script type="text/javascript">

function io_count(src, tgt) {
    if (src.nodeName && src.nodeName === "INPUT") {
        if (typeof tgt === "string") {
            tgt = document.getElementById(tgt);
        tgt.value = src.length;
        return false;
    return true;
<input id="txt_test1" onchange="return io_count(this, 'div_test1');" value="" />
<div id="div_test1"></div>
<input id="txt_test2" onchange="return io_count(this, 'div_test2');" value="" />
<div id="div_test2"></div>

Open in new window

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

LVL 31

Accepted Solution

Marco Gasi earned 500 total points
ID: 33424631
Try this, is tested.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  <meta http-equiv="content-type" content="text/html; charset=windows-1250">
  <meta name="generator" content="PSPad editor,">
    function changeContent(){
        var txt = document.getElementById('testtext').value;
        document.getElementById('dest').innerHTML = txt;
    function countChars(){
        var len = document.getElementById('testtext').value.length;
        document.getElementById('dest').innerHTML = len;
    <form name='test' action='test.html' method='post'>
    <input type="text" id="testtext" name="testtext" onkeyup="countChars();">
    <td id='dest'></td>

Open in new window

LVL 31

Expert Comment

by:Marco Gasi
ID: 33424772
Sorry, I forgot a little trick. Add this function within <script> tags:

    function emptyText(){
      document.getElementById('testtext').value = '';  

and add this to the <body tag:

<body onload="emptyText();">

This ensure that refreshing the page the text box is empty and the count begin from 0.

LVL 75

Expert Comment

by:Michel Plungjan
ID: 33429739

window.onload=function() {

LVL 31

Expert Comment

by:Marco Gasi
ID: 33436637
Still need you assistance with this?

Author Closing Comment

ID: 33436803

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Introduction This article is intended for those who are new to PHP error handling (  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
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.

734 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