0

User comment looks something like this:

<div id="comment23" class="commentholder">
    <p>
        This is a sample comment. It may contain different kinds of html.
        like <strong>strong text</strong>
        or anything that markdown supports.
    </p>
</div>

When text in comment23 div is selected, I would like to display a button near it. If button is pressed I want to take the selected text, prepend ">" to each line (to make it blockquote in markdown) and paste it in an input box.

How can I achieve this in javascript, preferably jquery?

DavidW
  • 5,069
  • 14
  • 46
  • 68

1 Answers1

1

You'll need to read up on Selection objects. If you want to support IE < 9, which has a completely different selection API from other browsers, you'll need to read up about that too. You'll also need to simulate the selectionchange event, which exists in WebKit and IE but not Firefox and Opera.

I've answered all the constituent parts of this on SO before. Here's a couple of links:

Community
  • 1
  • 1
Tim Down
  • 318,141
  • 75
  • 454
  • 536