|
| 1 | +// Get references to HTML elements |
| 2 | +const textInput = document.getElementById("text-input"); |
| 3 | +const wordCount = document.getElementById("word-count"); |
| 4 | +const charCount = document.getElementById("char-count"); |
| 5 | +const paraCount = document.getElementById("para-count"); |
| 6 | +const countButton = document.getElementById("count-button"); |
| 7 | +const clearButton = document.getElementById("clear-button"); |
| 8 | +const charLimitInput = document.getElementById("char-limit"); |
| 9 | +const highlightButton = document.getElementById("highlight-button"); |
| 10 | +const highlightWordInput = document.getElementById("highlight-word"); |
| 11 | + |
| 12 | +// Add an event listener to the count button |
| 13 | +countButton.addEventListener("click", countWords); |
| 14 | + |
| 15 | +// Add an event listener to the clear button |
| 16 | +clearButton.addEventListener("click", clearText); |
| 17 | + |
| 18 | +// Add an event listener to the highlight button |
| 19 | +highlightButton.addEventListener("click", highlightWords); |
| 20 | + |
| 21 | +// Add an event listener to the character limit input |
| 22 | +charLimitInput.addEventListener("input", checkCharacterLimit); |
| 23 | + |
| 24 | +// Save the text input and counts to local storage |
| 25 | +function saveData() { |
| 26 | + localStorage.setItem("text", textInput.value); |
| 27 | + localStorage.setItem("words", wordCount.textContent); |
| 28 | + localStorage.setItem("chars", charCount.textContent); |
| 29 | + localStorage.setItem("paras", paraCount.textContent); |
| 30 | +} |
| 31 | + |
| 32 | +// Load the saved data from local storage |
| 33 | +function loadData() { |
| 34 | + textInput.value = localStorage.getItem("text") || ""; |
| 35 | + wordCount.textContent = localStorage.getItem("words") || "Words: 0"; |
| 36 | + charCount.textContent = localStorage.getItem("chars") || "Characters: 0"; |
| 37 | + paraCount.textContent = localStorage.getItem("paras") || "Paragraphs: 0"; |
| 38 | +} |
| 39 | + |
| 40 | +// Function to count words, characters, and paragraphs |
| 41 | +function countWords() { |
| 42 | + const text = textInput.value; |
| 43 | + const words = text.split(/\s+/).filter((word) => word !== ""); |
| 44 | + const characters = text.length; |
| 45 | + const paragraphs = text |
| 46 | + .split("\n") |
| 47 | + .filter((para) => para.trim() !== "").length; |
| 48 | + |
| 49 | + wordCount.textContent = `Words: ${words.length}`; |
| 50 | + charCount.textContent = `Characters: ${characters}`; |
| 51 | + paraCount.textContent = `Paragraphs: ${paragraphs}`; |
| 52 | + |
| 53 | + saveData(); |
| 54 | +} |
| 55 | + |
| 56 | +// Function to clear the textarea and counts |
| 57 | +function clearText() { |
| 58 | + textInput.value = ""; |
| 59 | + wordCount.textContent = "Words: 0"; |
| 60 | + charCount.textContent = "Characters: 0"; |
| 61 | + paraCount.textContent = "Paragraphs: 0"; |
| 62 | + |
| 63 | + saveData(); |
| 64 | +} |
| 65 | + |
| 66 | +// Function to highlight specific words in the text |
| 67 | +function highlightWords() { |
| 68 | + const text = textInput.value; |
| 69 | + const wordToHighlight = highlightWordInput.value; |
| 70 | + const highlightedText = text.replace( |
| 71 | + new RegExp(wordToHighlight, "g"), |
| 72 | + '<span class="highlighted">$&</span>' |
| 73 | + ); |
| 74 | + |
| 75 | + // Display the highlighted text in the textarea |
| 76 | + textInput.innerHTML = highlightedText; |
| 77 | + |
| 78 | + saveData(); |
| 79 | +} |
| 80 | + |
| 81 | +// Function to check character limit |
| 82 | +function checkCharacterLimit() { |
| 83 | + const charLimit = parseInt(charLimitInput.value); |
| 84 | + const text = textInput.value; |
| 85 | + const characters = text.length; |
| 86 | + |
| 87 | + if (charLimit && characters > charLimit) { |
| 88 | + charCount.style.color = "red"; |
| 89 | + charCount.textContent = `Characters: ${characters} (Exceeding limit)`; |
| 90 | + } else { |
| 91 | + charCount.style.color = "black"; |
| 92 | + charCount.textContent = `Characters: ${characters}`; |
| 93 | + } |
| 94 | + |
| 95 | + saveData(); |
| 96 | +} |
| 97 | + |
| 98 | +// Load saved data when the page loads |
| 99 | +window.addEventListener("load", loadData); |
0 commit comments