Let's say I have a textarea and bold button for like that:
<div class="main">
<textarea cols="60" rows="12">
Lorem ipsum dolor sit amet...
</textarea>
</div>
<br>
<button onclick="bold()">Bold</button>
when I select (highlight) the content with the mouse and click on the bold button, I would like it to wrap the selection with tag, for example:
<b>content</b>
That's what I have so far but:
bold = function() {
var selection = window.getSelection().getRangeAt(0);
var selectedText = selection.extractContents();
var span = document.createElement("b");
span.appendChild(selectedText);
selection.insertNode(span);
}
- How can I make it to work with textrea too
- How can I make it to work only for the main div
jsfiddle: https://jsfiddle.net/5feLm4jn/3/