View Full Version : Counting remaining amount of words on mouseup event

01-23-2012, 11:17 AM
Hi all, as the title says i am trying to keep count of the number of words a user has typed into a textbox.

I have an emoticon box, something like the one that you see when you reply/start a thread here. The mouseup event fires off when the user clicks on an emoticon to display it in the textbox.

The problem here is, my code below does not start counting if an emoticon is added when theres nothing in the textbox (making the emoticon the first item). I can continue to add more smileys in and they will be counted in the word limit, but the first one will not be included in the count, unless the user clicks somewhere on the screen. How can i fix this problem?

//#message is the textbox
var characters = 100;
$("#remainder").append(characters+"words left");

var remaining = characters - $("#message").val().length;
$("#remainder").html(remaining+"words left");

01-24-2012, 11:29 AM

Philip M
01-24-2012, 11:37 AM
Reduce the number of allowed words to 99.

All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

01-24-2012, 11:18 PM
Yeah its tricky to keep track of the text entered, especially if people start cutting and pasting.

Instead of tracking the mouse, how about every x number of seconds you simply count the number of characters?

For example, we show the user how many characters they have entered by counting every 1 second. Works great for us.

01-25-2012, 01:37 AM
Philip M> Not sure if i understand the logic behind that, but i will try it.

Maize> That sounds great. but i am doing this for my chat program, and i am polling the database every 4s or so. I hope this method wont increase the lag for users.

01-25-2012, 03:05 AM
Not exactly sure how you're hooking up to the DB, but with todays(as in the past 8 years) computers, counting a input or textarea every second should not be an issue at all. Unless you're doing some other crazy stuff on the page as well!

Philip M
01-25-2012, 07:27 AM
Philip M> Not sure if i understand the logic behind that, but i will try it.

My point is that the number of words is only an imprecise estimate of the space taken. Small short words like this take less space than antidisestablishmentarianism and supercalifragilisticexpialidocious. So a 100-word limit is essentially arbitrary, so why not 99.

In any case, copy and paste make it very hard to count the number of words on entry.

01-26-2012, 02:07 AM
In the end i am now using a combination of keyup and mousemove to detect the remaining amount.

The problem with mouseup was that it does not add the emoticon that i am currently selecting to the count.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum