6

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!

chridam
  • 100,957
  • 23
  • 236
  • 235
Aaroniker
  • 170
  • 10
  • 31
  • Where is your try and code? – Top Questions Jun 16 '14 at 09:38
  • @idonteven in the fiddle, at least we have HTML code there. – King King Jun 16 '14 at 09:40
  • 1
    I think that comment is ironic...it´s just a div...there is no try or js... – Top Questions Jun 16 '14 at 09:43
  • i have no idea how to start, so no code so far :/ need some hints for the questions above – Aaroniker Jun 16 '14 at 09:45
  • You have a function to get selected text here ! http://stackoverflow.com/questions/24081277/detect-what-is-copied-from-webpage-with-jquery/24083504#24083504 – Joffrey Maheo Jun 16 '14 at 09:48
  • @JoffreyMaheo thanks, but i dont want to get the selected text only if someone copied it, isnt there any event that fires just after a selection? – Aaroniker Jun 16 '14 at 09:51
  • @Aaroniker : Try Google Dictionary chrome extension (https://chrome.google.com/webstore/detail/google-dictionary-by-goog/mgijmajocgfcbeboacabfgobmjgjcoja?hl=en), it exactly works the way you want your to work. Hope it helps. – PG1 Jun 16 '14 at 16:08
  • @ParagGangil thanks, but i want to write a jquery script for that, so that everyone can use this without the chrome extension – Aaroniker Jun 17 '14 at 08:24

4 Answers4

1

Selecting the text:

var text = $('text_element').text();

Selecting the html:

var html = $('text_element').html()

Appending data:

$('appending_element').append(text)
King King
  • 61,710
  • 16
  • 105
  • 130
Altus
  • 1,315
  • 3
  • 11
  • 19
1

If you want to append the div in the end of your div, use .append().

OR

If you want to add your div after some particular div, use .insertAfter().

PG1
  • 1,220
  • 2
  • 12
  • 27
1

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());
        });
    });
});
Shaunak D
  • 20,588
  • 10
  • 46
  • 79
0

May be this code will help you:-

<textarea cols="45" rows="5" id="message">
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
</textarea>
<script>

$("textarea").click(function(){
    var txt=document.getElementById("message");
    alert(txt.value.substr(txt.selectionStart, (txt.selectionEnd -txt.selectionStart)));
});
</script>
DWX
  • 2,282
  • 1
  • 14
  • 15