Chezshire Posted July 12, 2008 Share Posted July 12, 2008 I'm trying to make a work counter that i can apply to multiple instances <textareas) on the same page. I had made a live counter, but it wouldn't work once applied to more then one instance on a page. The one i'm currently working will work with multiple instances, although ti's not as spiffy as the one i'd originally wanted to use. The problem with it though is that it wants everything to be a 'form' and if everything isn't a form then it doesn't function. <HEAD> <SCRIPT LANGUAGE="JavaScript"> function CountWords (this_field, show_word_count, show_char_count) { if (show_word_count == null) { show_word_count = true; } if (show_char_count == null) { show_char_count = false; } var char_count = this_field.value.length; var fullStr = this_field.value + " "; var initial_whitespace_rExp = /^[^A-Za-z0-9]+/gi; var left_trimmedStr = fullStr.replace(initial_whitespace_rExp, ""); var non_alphanumerics_rExp = rExp = /[^A-Za-z0-9]+/gi; var cleanedStr = left_trimmedStr.replace(non_alphanumerics_rExp, " "); var splitString = cleanedStr.split(" "); var word_count = splitString.length -1; if (fullStr.length <2) { word_count = 0; } if (word_count == 1) { wordOrWords = " word"; } else { wordOrWords = " words"; } if (char_count == 1) { charOrChars = " character"; } else { charOrChars = " characters"; } if (show_word_count & show_char_count) { alert ("Word Count:\n" + " " + word_count + wordOrWords + "\n" + " " + char_count + charOrChars); } else { if (show_word_count) { alert ("Word Count: " + word_count + wordOrWords); } else { if (show_char_count) { alert ("Character Count: " + char_count + charOrChars); } } } return word_count; } // End --> </script> </HEAD> <!-- STEP TWO: Copy this code into the BODY of your HTML document --> <BODY> <form> <textarea cols=40 rows=5 name=x> </textarea> <br> <input type=button value="Count Words" OnClick ="CountWords(this.form.x, true, true);"> </form> <form> <textarea cols=40 rows=5 name=x> </textarea> <br> <input type=button value="Count Words" OnClick ="CountWords(this.form.x, true, true);"> </form> <textarea cols=40 rows=5 name=x> </textarea> <br> <input type=button value="Count Words" OnClick ="CountWords(this.form.x, true, true);"> You can see by going to http://www.xpg.us/cerebra/countwords.php that the first and second boxes work, but the third doesn't because i removed the 'form' tags - i need this to not be in a form so that it will work with my codes text areas. So to sum this up,i'm trying to find out how to make teh script work with text areas that are bracked/bookend by 'form tags' Thank you for any help offered. Link to comment https://forums.phpfreaks.com/topic/114358-solved-word-counter-doesnt-work-unless-as-a-form-helpadviceguidance-sought/ Share on other sites More sharing options...
rmbarnes82 Posted July 12, 2008 Share Posted July 12, 2008 Hi, Not strictly a PHP question, but meh. You problem is the way you call the CountWords function: <input type=button value="Count Words" OnClick ="CountWords(this.form.x, true, true);"> You use this.form.x to identify the text area, not no form exists. To fix this: 1. Give each text area a unique id, eg: <textarea id="text1"></textarea> 2. Call CountWords using the textarea's id, so for text1: <input type=button value="Count Words" OnClick ="CountWords(document.getElementById('text1'), true, true);"> Robin Link to comment https://forums.phpfreaks.com/topic/114358-solved-word-counter-doesnt-work-unless-as-a-form-helpadviceguidance-sought/#findComment-588082 Share on other sites More sharing options...
Chezshire Posted July 12, 2008 Author Share Posted July 12, 2008 Thank you Robin! Your answer provided me with what i've been struggling to work through for almost a week. Thank you thank you thank you a thousand times thank you! Link to comment https://forums.phpfreaks.com/topic/114358-solved-word-counter-doesnt-work-unless-as-a-form-helpadviceguidance-sought/#findComment-588121 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.