0

In a form's textarea, I'd like to allow users to select part of their text and by pressing a button, encompass the text with <strong> tags.

How to do this using jQuery?

Jand
  • 2,527
  • 12
  • 36
  • 66
  • 1
    what have you attempted so far? – Paul Fitzgerald Apr 20 '15 at 23:40
  • @Paul I tried to extract the relevant part from a lightweight text-editor but as I'm new to jQuery, it did not work at all. I also looked at several other SO questions but none addressed the `select text` part of my question. – Jand Apr 20 '15 at 23:43
  • You want to put the selected text between escaped `` tag like BBCode or make the text bold inside the textarea by using the tag ``? – Henrique Arthur Apr 20 '15 at 23:44
  • @theFX I merely want to inject html tags so that it appear bold after being rendered (in Django template). I don't want to make a WYSIWYG behavior. – Jand Apr 20 '15 at 23:47
  • Have you taught of using http://ckeditor.com/ ? – Vim Apr 20 '15 at 23:51
  • @Vim I just hate all WYSIWYG editors, so using any of them is out of question :) – Jand Apr 20 '15 at 23:52
  • Here is another link which discuss the same topic... http://stackoverflow.com/questions/9274120/how-can-i-display-bold-text-in-a-textarea. The outcome is, there is no way to do it as of now. – Vim Apr 20 '15 at 23:56
  • can you try this? http://jsfiddle.net/XCb95/80/ – Sushil Apr 21 '15 at 00:00
  • @Sushil that's exactly what I need dude! go ahead post it and I'll accept the answer. – Jand Apr 21 '15 at 00:03
  • thanks @Randi. let me post this as an answer. – Sushil Apr 21 '15 at 00:04

1 Answers1

3

you can try using selectionStart and selectionEnd properties to find the selected text. here's the jsfiddle http://jsfiddle.net/XCb95/80/

Sushil
  • 2,837
  • 4
  • 21
  • 29
  • Worked like a charm. Many thanks! I just needed to add `type="button"` into – Jand Apr 21 '15 at 00:51
  • you can create a normal input button then. – Sushil Apr 21 '15 at 16:02