How to Add Text Formatting to a Textarea using JavaScript? Last Updated : 14 Nov, 2024 Comments Improve Suggest changes Like Article Like Report To add text formatting (such as bold, italic, or inserting special characters) to a <textarea> using JavaScript, you can manipulate the selected text or insert formatted text at the cursor position. This can be done by directly modifying the value of the <textarea> based on user interactions.Example: Basic Text Formatting (Bold, Italic) in a TextareaHere's how you can implement basic text formatting functions:HTML Structure HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Textarea Text Formatting</title> </head> <body> <textarea id="myTextarea" rows="8" cols="50" placeholder="Type some text..."></textarea> <br> <button onclick="wrapText('**', '**')">Bold</button> <button onclick="wrapText('*', '*')">Italic</button> <button onclick="insertText('[Link text](url)')">Insert Link</button> <script> // Function to wrap selected text with start and end tags (e.g., for bold or italic formatting) function wrapText(startTag, endTag) { const textarea = document.getElementById('myTextarea'); const start = textarea.selectionStart; const end = textarea.selectionEnd; const text = textarea.value; // Get the selected text const selectedText = text.substring(start, end); // Replace the selected text with formatted text const formattedText = startTag + selectedText + endTag; textarea.value = text.substring(0, start) + formattedText + text.substring(end); // Reposition the cursor textarea.selectionStart = start; textarea.selectionEnd = start + formattedText.length; // Focus back on the textarea textarea.focus(); } // Function to insert text at the cursor position function insertText(textToInsert) { const textarea = document.getElementById('myTextarea'); const start = textarea.selectionStart; const end = textarea.selectionEnd; const text = textarea.value; // Insert the text at the cursor position textarea.value = text.substring(0, start) + textToInsert + text.substring(end); // Update cursor position textarea.selectionStart = textarea.selectionEnd = start + textToInsert.length; // Focus back on the textarea textarea.focus(); } </script> </body> </html> Output:OutputBold TextItalic TextInserting LinkExplanationwrapText(startTag, endTag) Function:This function wraps the selected text in the <textarea> with specified startTag and endTag.For example, clicking the "Bold" button wraps the selected text with ** (Markdown-style bold formatting).It preserves the cursor position after the text is modified.insertText(textToInsert) Function:This function inserts text at the cursor position in the <textarea>.Useful for inserting links, special characters, or custom templates.Example UsageBold: Select text in the <textarea> and click the "Bold" button to wrap the text with ** (e.g., **selected text**).Italic: Select text in the <textarea> and click the "Italic" button to wrap the text with * (e.g., *selected text*).Insert Link: Click the "Insert Link" button to insert a Markdown-style link at the cursor position.Customizing Formatting OptionsYou can extend the example by adding more buttons or functions for other formatting options, such as:Underline: Wrap text with <u> and </u> tags (or equivalent formatting).Strikethrough: Use ~~ for Markdown or <s> tags for HTML.This approach gives you full control over text manipulation within a <textarea> element using JavaScript Comment More infoAdvertise with us Next Article How to Add Text Formatting to a Textarea using JavaScript? A anuragtriarna Follow Improve Article Tags : JavaScript Web Technologies Web QnA Similar Reads How to change the shape of a textarea using JavaScript ? In General, Textarea will be in the form of rectangular or square shapes, but here we will deal with Unusual shapes of textarea which means this textarea will not be regular shapes but editable. Method 1: In the first method, let's see the irregular editable textarea created using CSS-shapes and con 2 min read How to Disable Textarea using JavaScript? This article will show you how to disable the textarea using JavaScript. Disabling a textarea using JavaScript can be done by setting the disabled property of the textarea element to true. This prevents the user from editing the content of the textarea. There are some reasons to use disabled textare 2 min read How to make a word count in textarea using JavaScript ? Counting words may be useful in scenarios where the user is recommended to enter a certain number of words and the word counter can keep track of the same. Below are approaches to make a word count in textarea using JavaScript:Table of Content Calculating the number of spaces present in the textSepa 4 min read How to create auto-resize textarea using JavaScript/jQuery ? Creating an auto-resize textarea using JavaScript or jQuery means dynamically adjusting the height of the textarea to fit its content as the user types. This enhances user experience by preventing the need for scrolling within the textarea, ensuring all content is visible.Table of ContentUsing JavaS 3 min read JavaScript - How to Change the Content of a <Textarea> ? Here are the various methods to change the content of a textarea using JavaScript.1. Using value PropertyThe value property is the most common method to change or retrieve the content of a <textarea>.HTML<textarea id="myTextarea">Initial content</textarea> <button onclick="chang 2 min read Like