Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

character count as I am typing into an input textbox

Posted on 2010-08-12
8
Medium Priority
?
767 Views
Last Modified: 2012-05-10
character count as I am typing into an input textbox
0
Comment
Question by:rgb192
[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 29

Expert Comment

by:Badotz
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;
}
</script>
...
<body>
<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>
...
</body>

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 31

Accepted Solution

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

Bye
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=windows-1250">
  <meta name="generator" content="PSPad editor, www.pspad.com">
  <title></title>
  <script>
    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;
    }
</script>
</head>
  <body>
    <form name='test' action='test.html' method='post'>
    <input type="text" id="testtext" name="testtext" onkeyup="countChars();">
    </form>
    <table>
    <tr>
    <td id='dest'></td>
    </tr>
    </table>
  </body>
</html>

Open in new window

0
 
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.

Bye
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 33429739
or

window.onload=function() {
 countChars();
    }

0
 
LVL 31

Expert Comment

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

Author Closing Comment

by:rgb192
ID: 33436803
thanks
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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…
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

721 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