2

Like SO has for example the B button:

**strong text**

But strong text to be automatically selected, and the cursor to be positioned just before the s

Alex
  • 64,868
  • 164
  • 416
  • 621

1 Answers1

2

You could use my Rangy Inputs jQuery plug-in now that I've finally got round to documenting it. Using it, the following will do the job:

$textArea = $("#yourtextarea");
$textArea.focus();
$textArea.surroundSelectedText("**", "**");

This will select the same text as was selected before, which is what SO does. However, if you prefer to have a caret before the "s" as you say then you can add the following:

$textArea.collapseSelection(true);

jsFiddle example: http://jsfiddle.net/AL7uY/

Tim Down
  • 306,503
  • 71
  • 443
  • 520