How to make selected text bold/italic/underlined in javascript
Loading
How to make selected text bold/italic/underlined in javascript
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Naimish MakwanaRank #324 · 7.7K reputationPosted Jan 31, 2023, 12:12 AM EST
Hello Ankush,
You can do as below.
Your HTML
CSS:
div.container { width: 300px; height: 100px; border: 1px solid #ccc; padding: 5px; } .bold{ font-weight:bold; } .italic{ font-style :italic; } .underline{ text-decoration: underline; }Javascript:
$(document).ready(function(){ $('.boldText').click(function(){ $('.container').toggleClass("bold"); }); $('.italicText').click(function(){ $('.container').toggleClass("italic"); }); $('.underlineText').click(function(){ $('.container').toggleClass("underline"); }); });Working Example : https://codepen.io/Naimishcon/pen/abjagJW
Thanks
Uday DodiyaRank #239 · 10.3K reputationPosted Jan 31, 2023, 1:59 AM EST
Hello Ankush,
Try Following Code
Bold Italic Underline Select some text in this area and click the buttons above to make it bold, italic, or underlined. var boldBtn = document.getElementById('boldBtn'); var italicBtn = document.getElementById('italicBtn'); var underlineBtn = document.getElementById('underlineBtn'); boldBtn.addEventListener('click', function() { document.execCommand('bold', false, null); }); italicBtn.addEventListener('click', function() { document.execCommand('italic', false, null); }); underlineBtn.addEventListener('click', function() { document.execCommand('underline', false, null); });Ankush Verma129 reputationPosted Jan 31, 2023, 12:01 PM EST
I have use this code . Please let me know how to assign tinymce function popupHTML this variable
const popupHTML = ``;
popupHTML=tinymce({
height: 500,
menubar: false,
plugins: [
'advlist autolink lists link image charmap print preview anchor',
'searchreplace visualblocks code fullscreen',
'insertdatetime media table paste code help wordcount'
],
toolbar: 'undo redo | formatselect | bold italic backcolor | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | removeformat | help'
});
Naimish MakwanaRank #324 · 7.7K reputationPosted Feb 1, 2023, 12:42 AM EST
Hello Ankush,
Please refer below link.
https://stackoverflow.com/a/67260808/3054222Thanks
Naimish
Naimish MakwanaRank #324 · 7.7K reputationPosted Feb 1, 2023, 5:11 AM EST
Hello Ankush,
To assign the
tinymcefunction to thepopupHTMLvariable, you can wrap thetinymcefunction call in an anonymous function and then assign the result to thepopupHTMLvariable:const popupHTML = (function() { return tinymce({ height: 500, menubar: false, plugins: [ 'advlist autolink lists link image charmap print preview anchor', 'searchreplace visualblocks code fullscreen', 'insertdatetime media table paste code help wordcount' ], toolbar: 'undo redo | formatselect | bold italic backcolor | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | removeformat | help' }); })();Thanks
Naimish