I want to implement the make bold
and put underline
functions on my own. For this, I need to get the text which is marked like this:
How can I do this with JavaScript?
I want to implement the make bold
and put underline
functions on my own. For this, I need to get the text which is marked like this:
How can I do this with JavaScript?
var start = element.selectionStart;
var end = element.selectionEnd;
var sel = element.value.substring(start, end);
Based on this and this questions, this fiddle demo shows how you can implement make bold and toggle bold functionality on selected text.
The js function to make selected text bold is:
function makeBold() {
var selection = window.getSelection();
if (selection.rangeCount) {
var range = selection.getRangeAt(0).cloneRange();
var newNode = document.createElement("b");
range.surroundContents(newNode);
selection.removeAllRanges();
selection.addRange(range);
}
}