Go Back   SitePoint Forums > Forum Index > Program Your Site > JavaScript
Newsletter FAQ Members List Calendar Mark Forums Read

New to SitePoint Forums? Register here for free!

SitePoint Sponsor
 
Reply
 
Thread Tools Display Modes
Old Jul 23, 2003, 06:05   #1
redemption
SitePoint Wizard
silver trophy
 
redemption's Avatar
 
Join Date: Sep 2001
Location: Singapore
Posts: 5,332
Looking for a textarea word count bookmarklet

Yeap title says it all: I've managed to find a character count bookmarklet but that is pretty much useless to me right now. I've also found one that reports the number of words in the selection.

If someone has come across one, let me know. And yes I've Googled for it
redemption is offline   Reply With Quote
Old Jul 23, 2003, 09:15   #2
beetle
Web-coding NINJA!
silver trophy
 
beetle's Avatar
 
Join Date: Jul 2002
Location: Dallas, TX
Posts: 2,910
Do you want it to prompt you for anything, like the name/id of the textarea? Or do you just want it to find a textarea arbitrarily?
beetle is offline   Reply With Quote
Old Jul 23, 2003, 09:35   #3
redemption
SitePoint Wizard
silver trophy
 
redemption's Avatar
 
Join Date: Sep 2001
Location: Singapore
Posts: 5,332
Don't tell me you're thinking of writing one for me again?

I'm thinking of something like the character count bookmarklet where the no. of characters appear next to every textfield or textarea it can find and change as you type. Though in reality there would be only 1 textarea that matters so having it work on just that one (probably using getElementsByTagName("textarea") would be enough).
redemption is offline   Reply With Quote
Old Jul 23, 2003, 09:41   #4
beetle
Web-coding NINJA!
silver trophy
 
beetle's Avatar
 
Join Date: Jul 2002
Location: Dallas, TX
Posts: 2,910
Did I already write one for you? *bad memory*

Can you link me to the two you've already found?
beetle is offline   Reply With Quote
Old Jul 23, 2003, 09:48   #5
redemption
SitePoint Wizard
silver trophy
 
redemption's Avatar
 
Join Date: Sep 2001
Location: Singapore
Posts: 5,332
Yup you did - remember the one for incrementing the fields after a particular one is changed?

http://www.squarefree.com/bookmarklets/forms.html has the character count one... I can't find the count words in selection one now, but I'll post the code

Code:
/* count words in selection */
EhV3eT8s=document.getSelection().replace(/\s/g,' ').replace(/  {2,}/g,' ').replace(/ $|^ /g,'').split(' ');
if(EhV3eT8s!=''){alert(EhV3eT8s.length+' words in selected text')}else{alert('Select some text first!')}
redemption is offline   Reply With Quote
Old Jul 23, 2003, 10:22   #6
beetle
Web-coding NINJA!
silver trophy
 
beetle's Avatar
 
Join Date: Jul 2002
Location: Dallas, TX
Posts: 2,910
Oh ya, I remember that. I guess I was thinking of bookmarklets in particular.

It was pretty easy to modify that character count one, only two lines of the code needed to change.

javascript:(function(){var D=document,i,f,j,e;for(i=0;f=D.forms[i];++i)for(j=0;e=f[j];++j)if(e.type=="textarea" )S(e);function S(e){if(!e.N){var x=D.createElement("span" ),s=x.style;s.color="green";s.background="white";s.font="bold 10pt sans-serif";s.verticalAlign="top";e.parentNode.insertBefore(x,e.nextSibling);functi on u(){l=e.value.split(/[\s\n]+/).length;x.innerHTML=(e.value=='')?0:l;}u();e.onchange=u;e.onkeyup=u;e.oninput =u;e.N=x;}else{e.parentNode.removeChild(e.N);e.N=0;}}})()

Works on the page I tested it on
beetle is offline   Reply With Quote
Old Jul 23, 2003, 10:42   #7
redemption
SitePoint Wizard
silver trophy
 
redemption's Avatar
 
Join Date: Sep 2001
Location: Singapore
Posts: 5,332
Thanks once again!

You're starting to become my JavaScript angel from heaven
redemption is offline   Reply With Quote
Old Jul 23, 2003, 10:56   #8
beetle
Web-coding NINJA!
silver trophy
 
beetle's Avatar
 
Join Date: Jul 2002
Location: Dallas, TX
Posts: 2,910
I'm here to help

You're quite welcome.
beetle is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread | Next Thread »

Thread Tools
Display Modes

 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Sponsored Links
 
Forum Jump


All times are GMT -7. The time now is 21:03.


Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Copyright 1998-2009, SitePoint Pty Ltd. All Rights Reserved