Login Register






[How To] Character Counter (jQuery) filter_list
Author
Message
[How To] Character Counter (jQuery) #1
Final result demo: http://imsanctuary.info/jq/char/

First, we'll start with our basic HTML doc.
Spoiler:
Code:
<!doctype html> <html lang="en"> <head> <title> Character Counter </title> <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.min.js"></script> </head> <body> <script type="text/javascript" src="charCount.js"></script> </body> </html>



Now add a textarea inside the <body> tags, for the text
The code for it would be:
Spoiler:
Code:
<textarea id="text" cols="60" rows="15" placeholder="Type here..."></textarea>



Now, we're going to add a <div> to display the number of characters we're typing.
Add this code below (or above, your choice) the <textarea> tags.
Spoiler:
Code:
<div> <p>Characters: <span id="num">0</span></p> </div>



Now you're going to make a new file named "charCount.js".
This will be the entire code inside that file (I'll explain what it does with comments):
Spoiler:
Code:
$('#text').keyup(function() { // When we type/del a char, the number changes var text = $('#text').val().length; // Taking the number from the textarea and giving us the length of it $('#num').html(text); // Displays the number of characters we've typed });



Save that, and you're done! Smile
CSS is optional, but is always nice to have.

Reply

RE: [How To] Character Counter (jQuery) #2
Nice tutorial. We could do this with PHP too by using the string length function but this is just as effective.
Thanks, Sanc. Confusedmile:

PHP Code:
echo strlen($word);

Edit: jQuery is probably the better option as it's a lot smoother and is eye candy.

Reply

RE: [How To] Character Counter (jQuery) #3
Nice tutorial Sanc, I'll try this out when I have time.
[Image: U2JKI5T.png]

Reply

RE: [How To] Character Counter (jQuery) #4
Thanks guys. Smile And yeah, I read somewhere about the sterl function in PHP, it's an alternative to this.
I'll make a tutorial on how to make a Word Counter as soon as I found out how, since those have more use. Tongue

Reply

RE: [How To] Character Counter (jQuery) #5
(07-17-2013, 04:03 PM)Sanctuary Wrote: Thanks guys. Smile And yeah, I read somewhere about the sterl function in PHP, it's an alternative to this.
I'll make a tutorial on how to make a Word Counter as soon as I found out how, since those have more use. Tongue

PHP Code:
<?php echo str_word_count("This line of code will display the number nine"); ?>

:tongue:

Edit: http://www.discussionzone.net/Thread-How...rd-Counter

Reply