Solved

JS Character Countdown (photo)

Posted on 2012-03-11
7
226 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
Envision that you are chipping away at another e-business site with a team of pundit developers and designers. Everything seems, by all accounts, to be going easily.
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.
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)

757 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now