JavaScript - Counting Occurences In Textarea
Hello all!
I've run into a few hour long snag and cant seem to get thru it. My homework says to write a JS program form with an identifying lable, an associated text area and 2 buttons. The buttons should have text that says "# letter occurrences" and one that says "# word occurrences". Each button should call an associated JS function. The # letter occurrences routine should print a table indicatin the number of occurrences of each letter off the alphabet in the text area, the # word occurrences routine should print a table indicating the number of one letter wors, the number of two letter words, the number of three letter words, up to 10 letter words ... So far here is what I have ..... <?xml version = "1.0" encoding = "utf-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns = "http://www.w3.org/1999/xhtml"> <head> <title>random</title> <script type="text/javascript"> var tableLetter="textArea"; var tableWord="textArea"; var lengthOfWord="tableWord.Length"; function ShowDiv(divName) { HideDivs(); if (document.all) { document.all.tableLetter.style.visibility = "visible"; } else { document.getElementById(divName).style.visibility = "visible"; } } function HideDivs() { if (document.all) { document.all.tableLetter.style.visibility = "hidden"; document.all.tableWord.style.visibility = "hidden"; } else { document.getElementById('tableLetter').style.visibility = "hidden"; document.getElementById('tableWord').style.visibility = "hidden";} } </script> </head> <body> <form name="form1" method="post" action=""> <textarea name="textArea" rows=6 cols=50> How many occurrences are in your sentence? </textarea> <br /> <input type="button" onClick="ShowDiv('tableLetter')" name="button1" value="# Letter Occurences"> <input type="button" onClick="ShowDiv('tableWord')" name="button2" value="# Word Occurences"> </form> <br /> <div id="text containter" style='width:420; height:85px; background:silver; position: absolute; '> <table> <div id='tableLetter' style="position:absolute; left:5px; top:10px"> <font color='#0000FF'>Letter Occurrences </font> </div> </table> <div id='tableWord' style='position:absolute; left:5px; top:10px; visibility:hidden;'> <font color='#0000FF'>Word Occurrences.</font> </div> </div> </body> </html> Similar TutorialsHi, I need to count how many times a keyword (input) appears in a string (str). I think I'm getting close with this: Code: var input = document.getElementById('autocomplete').value; var inp = input; for (var j = 0; j < gmarkers.length; j++) { var str=gmarkers[j].myname; var patt1=new RegExp(inp,'i'); if (str.match(patt1)) { found = true; var box = [inp]; var string1 = str; for (var i=0;i<box.length;i++) { nCount = string1.split(box[i]).length-1; alert(box[i] + ' is found ' + nCount + ' times<br>'); } but I can't get it to actually work. any ideas? thanks in advance. Hi i have a problem, i've been trying to fix this for the whole day pls see my code below Code: for ($o = 0; $o <= $totalclass; $o++) { for($i = 1; $i <= 45; $i++) { if ($_SESSION['classification'][$o] == $i) { $sql2="SELECT ClassDesc FROM tblclass WHERE ClassID = '$i'"; $result2=mysql_query($sql2); // If successfully queried if($result2) { while ($row2 = mysql_fetch_assoc($result2)) { $ClassDesc2 = $row2['ClassDesc']; } } //echo $ClassDesc2; ?> <tr> <td bgcolor="FAFAF6" class="small" valign="top">Class <? echo $i; ?></td> <input type="hidden" name="<? echo "classid[]"; ?>" value="<? echo $i; ?>"> <td bgcolor="FAFAF6"> <textarea name="<? echo "specification[]"; ?>" COLS="50" ROWS="6" class="small" wrap="virtual" tabindex="<? echo $i; ?>"><? echo $ClassDesc2;?></textarea> <input type="button" value="Reset" onclick="window.reset();" name="reset"> </td> </tr> <? } } } i've trying to create a button or image to reset one textarea (from whole array) and so far i've been unsuccessful. i've seen this on other website and i know it is possible to do this, pls help! I have a vertical menu in which the sub menu is a box that pops out to the right and the user can click boxes to make selections (input check boxes). I would like some way to indicate to the user after they have left the sub menu, that they have made selections within that sub menu. Perhaps a number after the menu item name, in which the number of sub menu selections are indicated in parenthesis? Is this possible? Code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Test Project</title> <script type="text/javascript"> var clicks=0; function linkClick(){ document.getElementById('clicked').value = ++clicks; } </script> </head> <body> <p align="center"><a href="http://www.google.ie" target="_blank" onclick="linkClick()">Google</a></p> <p align="center"> </p> <p align="center"><a href="http://www.google.ie" target="_blank" onclick="linkClick()">Google</a></p> <p align="center"> </p> <p align="center"><a href="http://www.google.ie" target="_blank" onclick="linkClick()">Google</a></p> <p align="center"> </p> <td colspan="3" valign="top">You have clicked my links <input id="clicked" size="1" onfocus="this.blur();" value="0" > times</td> <Script Language="JavaScript"> if ("clicks">3); { window.open('http://ie.yahoo.com','','scrollbars=no,menubar=no,height=600,width=800,resizable=yes,toolbar=no,location=no,status=no'); } </Script> </body> </html> I am a complete novice, I've spend most of today trying to research and learn Javascript. I would like my page to keep a count of how many times the links have been opened (this is happening) I would like a new pop up window to occur when the count gets to a specif value (this isnt happening) OR maybe to have a new hyperlink to appear that the user can then click on? Thanks in advance. Hi Im new to javascript and need some help with a project I have, the task I have is: Create a page that can take a bloke of text and out put the following o How many times each letter occurs o How many spaces o How many words o How many times each word occurs o A list of things that are not words or special characters I think maybe use an array but im not sure, like I said im new to this so not too sure on how to do it. I know it would read into a file say a txt file and sort through it this way, possibly using some kind of loop to read through all of the text before sorting it and I know it might use variables. I know how to kind of work through the task I have, but to actually implement the mechanics of the javascript is something that I then struggle with Thanks to all that help. Hi all, I have an array holding 100 randomly generated integers between 0-9 inclusive...firstArray[99] how do i use a second array to keep count of how many times each integer is generated..secondArray[9] I feel stupid for asking a question about searching arrays, when there's a very similar thread that has been answered just recently on the first page; however, I'm still having trouble contemplating my own scenario. Basically, my program prompts the user for the length of the array and then asks the user to fill the array with words. Here is where I need help: I want to confirm if the user wants to search the array for those words. If so, the user will then be prompted to enter the word he wishes to search for; if found, the location of that word will be reported and the number of times the word has been searched for will be kept track of in a separate array. Here is what I have so far: Code: /* -- phase 3 ------------------------------------------------------ search for words the user asks for */ // search variables var response; var search; while (true) { // confirmation protocol response = confirm("Do you wish to search the lexicon for a word?") if (response) { search = prompt("What word would you like to search for?"); // alert("search"); } else { alert("Thank you for wasting my time."); break; } // begin search protocol for (i=0; i<words.length; i++) { if (search==words[i]) { alert("Word found at" +i+);} } // end for loop else {alert("word not found.");} // counter array/accumulator var hits = new Array(words.length); hits[i] = 0; for (i=0; i<words.length; i++) { if (words[i] == search) { hits[i] = hits[i] + 1; alert("This word has been searched for " +hits[i]+ "times."); } } // end for loop } // end while The problem with my search seems to be that the search is parsed through the for loop; if it finds the word it alerts me that it was found at i location, but then it continues through and sees that the search does not equal the other values in the array and reports it's not found as well. My counter array is completely off, and I'm really at a loss to figure it out. I can see that the problem might be that each new search resets the hits[i] to equal 0, so no matter how many times a word is searched for, it returns a count of 1. I really want this array to track the number of hits for each word searched for, but have no clue why it's not working. Thanks for any help I can get; and please, feel free to critique my coding style, I definitely need to improve. How can i go about showing on screen a count from an initial value to an end value over a set time period. Initial value = 0 End value = 2000 Time = 25 seconds I cant find an example of this anywhere, so I'm trying here to see if somebody can help 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"); }); }); Hello I have this snippet for controlling the number of characters entered in a textarea. It works. But, if I replace in this code the name "my_form" by "contact-form" and "my_text" by "message", it doesnt ! It looks like the keyword is " my_form ". You change that , and the script will not work! It turns out that I have to change those names because the form as it was already had those names my_form and message before I added this counter And I dont want to replace the names of the form in order to adapt them to this code snippet below. The question is why would the script not work if I have just replaced two words, which dont seem to be keywords of JS programming (my_form and my_text) <html><head> <title>(Type a title for your page here)</title> <script language=JavaScript> <!-- function check_length(my_form) { maxLen = 50; // max number of characters allowed if (my_form.my_text.value.length >= maxLen) { // Alert message if maximum limit is reached. // If required Alert can be removed. var msg = "You have reached your maximum limit of characters allowed"; alert(msg); // Reached the Maximum length so trim the textarea my_form.my_text.value = my_form.my_text.value.substring(0, maxLen); } else{ // Maximum length not reached so update the value of my_text counter my_form.text_num.value = maxLen - my_form.my_text.value.length;} } //--> </script> </head> <body> <form name=my_form method=post> <textarea onKeyPress=check_length(this.form); onKeyDown=check_length(this.form); name=my_text rows=4 cols=30></textarea> <br> <input size=1 value=50 name=text_num> Characters Left </form> </body> </html> Hi all, I'm loading external xml and displaying specific tagged elements in a textarea. It works fine in IE because I can turn the variable object into text with a simple object.text property, but in Firefox (using the serializeToString method), the object appears in my textarea tags and all. The code looks something like this (this is just a dummy script - imagine the xml is already loaded into the object xmlDoc ): Code: function writePage(){ document.write('<textarea name="inputText">'+myText+'</textarea>'); } function xmlIsLoaded(){ if(window.ActiveXObject){ //tests for IE myText = xmlDoc.getElementsByTagName("desiredTag")[0].text; } else { //tests for FF myText = (new XMLSerializer()).serializeToString(xmlDoc.getElementsByTagName("desiredTag")[0]); } writePage(); } What am I missing here? I've tried toString, textContent, innerText, childNodes - just about everything I can think of to get just the text w/o the tags from the xml for the FF version. Any thoughts? ~gyz Hello there . Here is my simple code Quote: <html> <head> <script> function addText(){ document.getElementById("textarea").value+=document.getElementById("text").value+'\n'; document.getElementById("text").value=""; document.getElementById("text").onblur=document.getElementById("text").value=" Your Message..."; } </script> <style> #textarea{background-color:#F3F3F3; } </style> </head> <body> <form> <TEXTAREA id="textarea" COLS="80" ROWS="20" ></TEXTAREA><br/><br/> <input type="text" id="text" name="text" value=" Your Message..." onclick="this.value=''" size="96"> <input type="button" value="Send" name="submit" onclick="addText()"> </form> </body> </html> My problem is I dont know what to do iorder to make my send button to push the text entered in the text field from bottom to the top (not from the top to the bottom) I am trying to figure out how to make a random number I can plug into a script count down from that number at certain times of the day until it reaches 0. I would like it to reset itself at midnight every day. I'm trying to make it work with a script I found on here that resets itself at midnight every day. So instead of it counting down too fast, it would count down to the next number after a randomly generated number of minutes until it reaches 0. But it wouldn't necessarily have to end at 0 at midnight. It could go from 845 to 323 at the end of the day at a slower pace. Is that possible?
I found a script that is perfect for my need but it is written for forms textareas and I can seem to get it to work textareas that are not wrapped in form. Code: Code: <script>function insertAtCursor(sTag,eTag) { var obj = document.form.message; var strPos; var ie_strPos; var oSel; obj.focus(); strPos += sTag.length + eTag.length; ie_strPos += sTag.length + eTag.length; if (document.selection && document.selection.createRange) { // Internet Explorer oSel = document.selection.createRange(); if (oSel.parentElement() == obj) { oSel.text = sTag + oSel.text + eTag; ie_strPos += oSel.text.length; } oSel.moveStart ('character', ie_strPos); oSel.moveEnd ('character', 0); oSel.select (); } else if (typeof(obj) != "undefined") { // Firefox var longueur = parseInt(obj.value.length); var selStart = obj.selectionStart; var selEnd = obj.selectionEnd; obj.value = obj.value.substring(0,selStart) + sTag + obj.value.substring(selStart,selEnd) + eTag + obj.value.substring(selEnd,longueur); strPos += txtarea.value.substring(selStart,selEnd).length; obj.selectionStart = strPos; obj.selectionEnd = strPos; obj.focus (); } else obj.value += sTag + eTag; obj.focus(); } </script> <button onclick="insertAtCursor('[blah]','[/blah]')">Test</button> <form method="post" name="form" action=""> <textarea name="message" cols="50" rows="10"></textarea> </form> My textarea: Code: <textarea id="new_topic_text" class="new-topic-subject-text elastic" style="overflow: hidden;"></textarea> Hi guys, My brain is not working. May be I need a coffee break. I need your expertise. Below is the code holding the textarea. Look for the element with the id : themessage Code: <table width="100%" border="0"> <tr><td rowspan="4"><input type="button" name="send_email" id="send_email" style="height:50px; width:100px;" value="SEND" onClick="return err_check();" /></td><td><span class="que">From : </span></td><td colspan="3"><?php echo $from_email; ?><input type="hidden" name="from_mail" id="from_mail" value="<?php echo $from_email;?>"/><input type="hidden" name="camp_id" id="camp_id" value="<?php echo $camp_id;?>"/></td></tr> <tr><td><span class="que">To : </span></td><td colspan="3"><input name="to_email" id="to_email" style="width:250px;" /></td></tr> <tr><td><span class="que">CC : </span></td><td colspan="3"><input name="cc_email" id="cc_email" style="width:250px;"/></td></tr> <tr><td><span class="que">Subject : </span></td><td colspan="3"><input style="width:300px;" name="subject" id="subject" /></td></tr> <tr><td rowspan="1" colspan="2"> </td><td><input type="checkbox" name="ori_pdf" id="ori_pdf" /> PDF Quotation</td><td> </td><td> </td></tr><tr><td colspan="2"><span class="que">Credit Application</span></td><td><input type="checkbox" name="corporate" id="corporate"/>Corporate</td><td><input type="checkbox" name="individual" id="individual" />Individual</td><td><input type="checkbox" name="cash" id="cash" />Cash Account</td> </tr> <tr> <td colspan="2" rowspan="3"></td><td><input type="checkbox" name="tabloid" id="tabloid" />Tabloid Example</td> <td><input type="checkbox" name="broadsheet" id="broadsheet" />Broadsheet Example</td></tr> <tr><td><input type="checkbox" name="colmt" id="colmt" />Column Sizes Tabloid</td> <td><input type="checkbox" name="colmb" id="colmb" />Column Sizes Broadsheet</td></tr> <tr><td><input type="checkbox" name="maps" id="maps" />Maps / Distribution</td><td colspan="2" align="right">External Attachments <input id="upload_file" name="upload_file" type="file"/> </td></tr> <tr><td colspan="2"><span class="que">Message :</span></td><td colspan="3"> <textarea id="themessage" name="themessage" rows="15" cols="80" style="width: 100%"> </textarea> </td></tr> </table> Now on a button click, I'm trying to pass some values which one of them happens to be what ever that is entered in the textarea. For some reason, the stupid JS is not reading. But I think it is me that is stupid, not JS. Below is the code i'm running to get the value. Code: var message = document.getElementById('themessage').value; alert(message); The alert simply returns blank. There is an alert, but no value even where there is text in the text area. Can someone please shed some light on this. pretty please I am fairly new at Javascript, but there is something happening I do not understand. Please someone explain it to me and help with a solution. In the function, I pull data from an URL and I have done this with several different data fields and populate the HTML input, option, and check boxes with no problem with the following method. Code: document.getElementById("othertxt").value=strValue; document.getElementById("radsig").checked=true; document.getElementById("eventtime").value=strValue; But for some reason this method for textarea does not work and I know that there is a value in strValue because check it right before the line of code. Code: document.getElementById("othertxt").value=strValue; Is textarea just treated differently? I have searched the internet and seen examples but for some reason I can not find the correct solution. so I am creating a silly word ;irate word interpreter page, and I need some logic behind how to add text to a textarea each time I click on a button. Each time the user clicks on the button, the value is inserted into the text area. You should be able to click on multiple buttons to add to the last text that appears. Then I have a delete translation button when I want to clear the textarea field. A good example would be if the user clicks on the button with the value "sir", than "matey" will appear in the textarea. Now, if the user clicks on the button with the value "your", then "yer" will come up after matey. It's like creating a sentence on your own, and then deleting it when your done. Code: <body> <div id="page"> <div> <img style="float:left" src="slue.gif" width="205" height="190" alt=""> <div> <h1>Pirate Translator</h1> <p>Simply click on the buttons to translate words and/or phrases from English to pirate talk</p> </div> </div><br /> <hr /> <form name="pirate"> <div> <fieldset> <legend><b>Greetings:</b></legend> <input type="button" id="greeting" value="hello" /> <input type="button" id="greeting" value="pardon me" /> <input type="button" id="greeting" value="excuse me" /> <input type="button" id="greeting" value="" /> <input type="button" id="greeting" value="" /> <input type="button" id="greeting" value="" /> </fieldset> </div> <div> <fieldset> <legend><b>People:</b></legend> <input type="button" id="people" value="sir" /> <input type="button" id="people" value="madam" /> <input type="button" id="people" value="miss" /> <input type="button" id="people" value="stranger" /> <input type="button" id="people" value="officer" /> <input type="button" id="people" value="" /> <input type="button" id="people" value="" /> </fieldset> </div> <div> <fieldset> <legend><b>Questions:</b></legend> <input type="button" id="question" value="where is" /> <input type="button" id="question" value="can you help me" /> <input type="button" id="question" value="is that" /> <input type="button" id="question" value="how far is it to" /> <input type="button" id="question" value="" /> <input type="button" id="question" value="" /> <input type="button" id="question" value="" /> <input type="button" id="question" value="" /> </fieldset> </div> <div> <fieldset> <legend><b>Articles:</b></legend> <input type="button" id="article" value="the" /> <input type="button" id="article" value="my" /> <input type="button" id="article" value="your" /> <input type="button" id="article" value="" /> <input type="button" id="article" value="" /> <input type="button" id="article" value="" /> <input type="button" id="article" value="" /> </fieldset> </div> <div> <fieldset> <legend><b>Adjectives:</b></legend> <input type="button" id="adject" value="old" /> <input type="button" id="adject" value="attractiv" /> <input type="button" id="adject" value="happy" /> <input type="button" id="adject" value="" /> <input type="button" id="adject" value="" /> <input type="button" id="adject" value="" /> <input type="button" id="adject" value="" /> </fieldset> </div> <div> <fieldset> <legend><b>Places:</b></legend> <input type="button" id="places" value="restroom" /> <input type="button" id="places" value="restaurant" /> <input type="button" id="places" value="mall" /> <input type="button" id="places" value="hotel" /> <input type="button" id="places" value="pub" /> <input type="button" id="places" value="" /> <input type="button" id="places" value="" /> </fieldset> </div> <div> <fieldset> <legend><b>Desires:</b></legend> <input type="button" id="desire" value="I would like to" /> <input type="button" id="desire" value="I desire" /> <input type="button" id="desire" value="I wish I knew how to" /> <input type="button" id="desire" value="my mother told me to" /> <input type="button" id="desire" value="my campanion tells me to" /> <input type="button" id="desire" value="" /> <input type="button" id="desire" value="" /> </fieldset> </div> <div> <fieldset> <legend><b>Actions:</b></legend> <input type="button" id="action" value="find" /> <input type="button" id="action" value="take a nap" /> <input type="button" id="action" value="kill" /> <input type="button" id="action" value="hurt you" /> <input type="button" id="action" value="beat you up" /> <input type="button" id="action" value="go to the bathroom" /> <input type="button" id="action" value="" /> </fieldset> </div> <div> <p id="output"><textarea id="task_list" rows="5" cols="130"></textarea></p> <p id="output"><input type="button" id="" value="Delete Traslations" /></p> </div> </div> </form> <script src="js/translate.js"></script> </body> </html> I can write all kinds of stuff, but one thing is failing me, and is extremely frustrating. document.getElementById('fakeid').value; which is my least preferred option, won't even work on this text area. Either will: Code: var objElem = document.getElementById(strId); var objElem = objElem.getElementsByTagName('textarea'); var strComment = objElem[0].value; It will work fine in IE but will not in Firefox. I cannot understand why as I've done this many times before. The code it is using is: Code: <div id="commentAdd"><h3 id="commentHeader"><a href="javascript:;" onclick="func_toggle_visibility("commentAddContent");return false;">Add a comment</a></h3> <div class="contentnopadding" id="commentAddContent" style="display:block;"> <form id="formCommentAdd"> <div class="comleft"> <table> <tr><td><span class="subhead">Add Comment:</span></td><td><textarea id="fakeid" rows="3" cols="35"></textarea></td></tr> </table> </div> <div class="content"> <input id="commentAddSubmit" type="button" value="Submit Comment" onclick="commentInitAjax(this.id,'comment','1',false);"> </div> </form></div></div><br><br> <div class="contentnopadding"></div> hitting submit triggers that function (which i haven't pasted all of it, just the necessary parts). I have an alert right after where i assign the value, but it does not work in FF. Please help, it's giving me grief (if it helps, the HTML is written by javascript, but i can't see how it would have any effect). Hi all. I have a textfield on my form problem. My boss wants it as a textfield to keep the field small on the page but still wants to be able to read all of the information that is in the field. What can I do with this?? Can we have change to textarea when he hovers to clicks in it. Any help would be appreciated. I'm adding text into a text area using document.createTextNode Code: document.getElementById('controlQueueDump').appendChild(document.createTextNode(args+';')); But whenever the user edits the text box, it will never update anything again. I can add as much as I want, but when a user edits it (for example removes the contents), BAM, it is no longer updated. it's a textarea so it should be appending the child to the value attribute. Thanks a lot. |