?
Solved

javascript count characters in an input textbox

Posted on 2010-08-18
5
Medium Priority
?
633 Views
Last Modified: 2012-08-13
this counts characters
in an input textbox

but I want to display the character count next to the textfield
in the form

because I have a large form


<!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
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
5 Comments
 
LVL 19

Expert Comment

by:Michael701
ID: 33468731
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 1000 total points
ID: 33468921
Check this :

< div>< input type="text" id="testtext" name="testtext" onKeyUp="countChars();">< span id='dest'>< /span>< /div >

page updated :


<!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'>
    <div><input type="text" id="testtext" name="testtext" onKeyUp="countChars();"><span id='dest'></span></div>
    </form>
    <table>
    <tr>
    
    </tr>
    </table>
  </body>
</html>

Open in new window

0
 
LVL 9

Assisted Solution

by:Snarfles
Snarfles earned 1000 total points
ID: 33471399
Try this. Tested and working ->http://snarfles.net/code/count.html
<!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(len){
        //var txt = document.getElementById('testtext').value;
        document.getElementById('dest').innerHTML = len+" characters";
    }
    function countChars(){
        var len = document.getElementById('testtext').value.length;
        //document.getElementById('dest').innerHTML = len;
	changeContent(len);
    }
</script>
</head>
  <body>
    <form name='test' action='test.html' method='post'>
    <input type="text" id="testtext" name="testtext" onkeyup="countChars();"><span id="dest"></span>
    </form>
    <table>
    <tr>
    <td id='dest'></td>
    </tr>
    </table>
  </body>
</html>

Open in new window

0
 

Author Closing Comment

by:rgb192
ID: 33495876
thanks
0
 
LVL 82

Expert Comment

by:leakim971
ID: 33496043
Thanks for the points!
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 how to implement server side field validation and display customized error messages to the client.
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 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)

719 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