When working with text in JavaScript, you may come across situations where you need to add a prefix or suffix to each new line in a given text. In this tutorial, we'll walk you through a simple and effective way to achieve this using HTML, CSS, and JavaScript. We'll create an easy-to-use online tool that adds a specified prefix and suffix to each new line of text entered by the user. Step 1: Create the HTML Structure First, let's create the basic for our tool. This includes a text area for inputting the text, two input fields for the prefix and suffix, a button to trigger the , and an output text area to display the processed text. HTML structure JavaScript function <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Add Prefix and Suffix</title> </head> <body> <h1>Add Prefix and Suffix to Each New Line</h1> <textarea id="inputText" rows="10" cols="50" placeholder="Enter your text here..."></textarea> <br> <label for="prefix">Prefix:</label> <input type="text" id="prefix" /> <br> <label for="suffix">Suffix:</label> <input type="text" id="suffix" /> <br> <button onclick="addPrefixAndSuffix()">Add Prefix and Suffix</button> <br> <h2>Output:</h2> <textarea id="outputText" rows="10" cols="50" readonly></textarea> </body> </html> Step 2: Add CSS for Styling Although not required, you may want to add some basic the tool. Here's a simple example: CSS to style <style> body { font-family: Arial, sans-serif; max-width: 800px; margin: 0 auto; padding: 20px; } textarea, input { display: block; width: 100%; margin-bottom: 10px; } button { background-color: #4CAF50; color: white; padding: 8px 16px; border: none; cursor: pointer; text-decoration: none; } button:hover { background-color: #45a049; } </style> Add the above CSS code inside the tag of the HTML code to apply the styles. <head> Step 3: Write the JavaScript Function Now, let's write the JavaScript function that will add the specified prefix and suffix to each new line of the input text. Be sure to include this code within tags at the end of your HTML file, just before the closing tag. <script> </body> function addPrefixAndSuffix() { const inputText = document.getElementById('inputText').value; const prefix = document.getElementById('prefix').value; const suffix = document.getElementById('suffix').value; const lines = inputText.split('\n'); const outputLines = lines.map(line => { if (line.trim() === '') { return line; } return prefix + line + suffix; }); const outputText = outputLines.join('\n'); document.getElementById('outputText').value = outputText; } This function retrieves the input text, prefix, and suffix from the HTML elements. It then splits the input of lines, processes each line by adding the prefix and suffix (skipping empty or whitespace-only lines), and finally combines the processed lines back into a single string. text into an array Step 4: Test the Tool To test the , save the HTML, CSS, and JavaScript code in a new file and open it in your web browser. You should see a simple interface with input fields for the text, prefix, and suffix, as well as a button to trigger the function. tool .html Enter your text in the text area, input your desired prefix and suffix, and click the "Add Prefix and Suffix" button. The processed text with the added prefix and suffix should appear in the output text area. Conclusion In this tutorial, we have demonstrated how to create a user-friendly online tool that adds a prefix and suffix to each new line of text using HTML, CSS, and JavaScript. This tool can be helpful for tasks such as formatting lists, adding , or enhancing the appearance of your text. code snippets With this simple yet effective solution, you can save time and effort when working with text in JavaScript.