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
Solved

JS Character Countdown (photo)

Posted on 2012-03-11
7
230 Views
Last Modified: 2012-03-18
Hi, i am trying to find a good JS character countdown script that i can use to count down the characters remaining in a text area field that i am using on my site.

I have done a lot of searching on google but a lot of them seem to have very long scripts and others scripts are over 6 years ago.

Can someone recommend a good, robust character countdown timer that works very well and does not require a lot of resources when it is running. I was hoping to find something similar to the twitter script (please see attached)
0
Comment
Question by:oo7ml
  • 4
  • 3
7 Comments
 
LVL 1

Accepted Solution

by:
ravi_kaalla earned 500 total points
ID: 37706952
<html>
<body>
	<textarea id="textArea" rows="2" cols="20" onKeyUp="fnTextAreaChanged(this, 30);"></textarea>
	<br/>
	Characters left : <div id="count"></div>

	<script>
		function fnTextAreaChanged(objTA, intMaxLimit)
		{
			var intDiff = intMaxLimit - objTA.value.length;
			document.getElementById("count").innerHTML = intDiff;
		}
	</script>
</body>
</html>

Open in new window

0
 

Author Comment

by:oo7ml
ID: 37706995
Cool, thanks, can i ask what i should be looking for in all the different scripts... this is another one that i got

<html>
<head>
</head>
<body>
<textarea id="comments"></textarea>
<script>
document.getElementById('comments').onkeyup = characterCount;

function characterCount() {
    var container = this.nextSibling;
    if (!container || container.className !== 'counter') {
        container = document.createElement('div');
        container.className = 'counter';
        this.parentNode.insertBefore(container, this.nextSibling);
    }
    container.innerHTML = this.value.length;
}
</script>
</body>
</html>

Open in new window

0
 
LVL 1

Expert Comment

by:ravi_kaalla
ID: 37708333
This one also works fine. But, I recommend the first solution than second one.

Reasons:
1) Readability is better in first solution.
2) "DIV" tag is created on each key stroke which is overhead for JavaScript in second solution.
0
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
LVL 1

Expert Comment

by:ravi_kaalla
ID: 37708338
Below is the modified version of your code(second solution) as you required.
<html>
<head>
</head>
<body>
<textarea id="comments"></textarea>
<script>
document.getElementById('comments').onkeyup = characterCount;

function characterCount() {
	var intMaxCount = 30;
    var container = this.nextSibling;
    if (!container || container.className !== 'counter') {
        container = document.createElement('div');
        container.className = 'counter';
        this.parentNode.insertBefore(container, this.nextSibling);
    }
    container.innerHTML = ("Number of characters left : " + (intMaxCount - this.value.length));
}
</script>
</body>
</html>

Open in new window

0
 

Author Comment

by:oo7ml
ID: 37709133
Ok cool, thanks... so which script out of all of these do you recommend now:

1 - your first script
2 - the script i then posted
3 - your modified version of the script that i posted

Which of the above 3 do you suggest i use, thanks in advance
0
 
LVL 1

Expert Comment

by:ravi_kaalla
ID: 37709747
I prefer first script
0
 

Author Closing Comment

by:oo7ml
ID: 37709892
thanks for your help
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
key press alert 2 32
Add shadow behind div 5 25
ASP Classic - JavaScript - Get value from form in iFrame and pass to main page. 2 21
Html value of radio 14 27
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…

840 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