سؤال

When I select some text in the <div>, I want that highlighted text to appear in the textbox which is just below the div. How can I do it?

<div>
    My text goes here.
</div>
<asp:TextBox ID="txt" runat="server"/>
هل كانت مفيدة؟

المحلول

working demo http://jsfiddle.net/KgtW5/ or using DIV demo http://jsfiddle.net/KgtW5/3/

.on API: http://api.jquery.com/on/

I have customized it for your need man.

Good link: and BIG hint: Get the Highlighted/Selected text

Hope demo helps you, lemme know if I missed anything! :)

code

$('textarea').on('select', function() {
    var foo = getSelectionText();
    $('#hulk').val(foo);
});


function getSelectionText() {
    var text = "";
    if (window.getSelection) {
        text = window.getSelection().toString();
    } else if (document.selection && document.selection.type != "Control") {
        text = document.selection.createRange().text;
    }
    return text;
}​

html

<textarea>Some default text; HUlk is very cool innit</textarea>
<br/>

<input type="text" id="hulk" />
​

Image

enter image description here

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top