/    Sign up×
Community /Pin to ProfileBookmark

Help with character counter

Hi all,

I’ve successfully implemented a text character counter JS code I picked up on the web, and I was trying to add a spin to it: if the field already has data, tell me how many characters I’ve left based on the current value. Here’s the original code:

function taLimit() {
var taObj=event.srcElement;
if (taObj.value.length==taObj.maxLength*1) return false;
}

function taCount(visCnt) {
var taObj=event.srcElement;
if (taObj.value.length>taObj.maxLength*1) taObj.value=taObj.value.substring(0,taObj.maxLength*1);
if (visCnt) visCnt.innerText=taObj.maxLength-taObj.value.length;
}

I figured all I needed to do was add this…

function ShowMeFirst(visCnt) {
var taObj=event.srcElement;
if (taObj.value.length>0) visCnt.innerText=taObj.value.length;
}

…and then have that code run when the textarea loads. As in: onload=”return ShowMeFirst(MyCounter)”.

Alas, this isn’t working and I don’t know why!! Help, please!!

Me ?

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@NatuScapeauthorJul 22.2006 — By the way... I can work around this by using ASP code to tell the "MyCounter" span area to display the number of characters used; but I was wondering how to do that with the JS code.
Copy linkTweet thisAlerts:
@konithomimoJul 22.2006 — I am not exactly sure why you have the code that you do, but if you want you can simply use the following code and modify it:
<html>
<head>
<script type="text/javascript">
function showLeft(obj)
{
var n = obj.maxLength;
alert('You have '+(n-obj.value.length)+ ' characters left.');
}
</script>
</head>
<body>
<form>
<input type="text" value="" maxLength="30" onkeyup="showLeft(this)">
</form></body>
</html>
Copy linkTweet thisAlerts:
@NatuScapeauthorJul 22.2006 — Sorry, I forgot to add that what I do with the visCnt variable is display it with a span tag near the text area. I don't really want a pop-up alert to show up every time the person types.
Copy linkTweet thisAlerts:
@cridleyJul 23.2006 — Just set the value or innerHTML of that span tag insteade of display the alert. As konimothimo said, you can modify his code.

obj.form.span_name.innerHTML = 'You have '+(n-obj.value.length)+ ' characters left.';

or whatever...
×

Success!

Help @NatuScape spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.7,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...