Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Aug 2011
    Posts
    49
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Counting remaining amount of words on mouseup event

    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?

    Code:
    //#message is the textbox
    $(document).ready(function()
    {
        var characters = 100;
        $("#remainder").append(characters+"words left");
    
        $("#emoticonsbox").mouseup(function()
        {
            var remaining = characters - $("#message").val().length;
        	$("#remainder").html(remaining+"words left");
        });
    });

  • #2
    New Coder
    Join Date
    Aug 2011
    Posts
    49
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Bump.

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,159
    Thanks
    203
    Thanked 2,549 Times in 2,527 Posts
    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.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #4
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #5
    New Coder
    Join Date
    Aug 2011
    Posts
    49
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.

  • #6
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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!

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,159
    Thanks
    203
    Thanked 2,549 Times in 2,527 Posts
    Quote Originally Posted by capypara View Post
    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.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #8
    New Coder
    Join Date
    Aug 2011
    Posts
    49
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •