0
var editor = document.getElementById ("editor-content");
var bold = document.createEvent('TextEvent');
bold.initTextEvent('textInput', true, true, null, "**"+"boldtext"+"**");
editor.dispatchEvent(bold);

I am currently making a markdown editor with a textarea.

When a button is pressed, the above code will run.

It runs perfectly but I want the "boldtext"to be highlighted(selected) when it is inserted into the textarea.

How would I do this? Right now the cursor is put at the end of **boldtext**.

Like this.

**boldtext|**

Sang Yoo Kim
  • 365
  • 1
  • 15

2 Answers2

0

There is no way to display only one/few words in bold in a textarea unless you do something similar to what is mentioned: How can I display bold text in a textarea?

Community
  • 1
  • 1
depperm
  • 10,606
  • 4
  • 43
  • 67
  • This is not what I'm asking. The textarea will show `**bold text**` by itself. What I want to do is when the button is pressed, the string 'bold text' will be selected. `**bold text**` is what is getting inserted into the textarea. – Sang Yoo Kim May 28 '15 at 18:16
  • try this: http://stackoverflow.com/questions/3085446/selecting-part-of-string-inside-an-input-box-with-jquery – depperm May 29 '15 at 23:24
0

I have used

editor.selectionStart -= 10;
editor.selectionEnd   -= 2;

This sets caret beginning to end relatively.

**boldtext**-10 to -2 is from after t to b

Sang Yoo Kim
  • 365
  • 1
  • 15