Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append div to selected text

I want to show some buttons if someone select text in a editable area - like this:

enter image description here

So, I think I have to use <div contenteditable="true"> instead of <textarea>

But to be honest I have no idea how to append the div

Questions:

  1. Whats the (jquery) event for selecting text?
  2. How to append a div after select to this area?

JsFiddle: http://jsfiddle.net/hA7Zn/

Thanks for any hints!

like image 943
Aaroniker Avatar asked Jun 16 '14 09:06

Aaroniker


People also ask

How to append text in div?

Use the insertAdjacentText() method to append text to a div element, e.g. container. insertAdjacentText('beforeend', 'your text here') . The method inserts a new text node at the provided position, relative to the element it was called on.

What is the difference between append() and appendChild()?

Differences from Node.appendChild() : Element.append() allows you to also append string objects, whereas Node.appendChild() only accepts Node objects. Element.append() has no return value, whereas Node.appendChild() returns the appended Node object.


1 Answers

Check this Demo for getting selected text,

Event selectstart is fired in case of selection

$(function () {
    $('div').on('selectstart', function () {
        console.log('..');
        $(document).one('mouseup', function() {
            alert(this.getSelection());
        });
    });
});
like image 114
Shaunak D Avatar answered Oct 28 '22 21:10

Shaunak D