0

I want to be able to bold and underline on button click, but only apply to the text that is highlighted in a textarea. I've seen some people use window.getSelection() and I plugged it in but it wasn't working. I'll show you what I'm dealing with.

function bold() {
  var selection = window.getSelection()
  selection.style.fontWeight = "bold"
}
<textarea id="theType"></textarea>
Jay Lane
  • 1,379
  • 15
  • 28
Ryan C
  • 1
  • 1
  • 8
    A textarea element can only contain plain text, no formatting of any kind – j08691 Nov 01 '16 at 18:25
  • then what can I use? – Ryan C Nov 01 '16 at 18:27
  • 2
    Google for "HTML rich text editor". There is no trivial solution. – JJJ Nov 01 '16 at 18:29
  • You can use `contenteditable`(without `textarea`) and then you'd approach it as you're now. But I agree with@JJJ that WYSIWYG editor is better – eithed Nov 01 '16 at 18:31
  • Yeah, `contentEditable` property to a div and then `selection.execCommand('bold')` to make them bold or underline or whatever. Note that it may behave differently on different browsers. https://msdn.microsoft.com/en-us/library/ms536419(v=vs.85).aspx – mojarras Nov 01 '16 at 18:51
  • Not quite an exact duplicate, but it should be clear from the linked question that what you're asking is not possible using ` – zzzzBov Nov 01 '16 at 19:15

0 Answers0