Referencing the selected text in a textarea

241 Views Asked by At

Is there a way to retrieve the text that's currently selected in a text area in Seaside?

2

There are 2 best solutions below

0
On BEST ANSWER

Sorry, pasted it in the wrong place, here's the solution:

MyComponent >> script
 ^ 'function selectedText(input){
    var startPos = input.selectionStart;
    var endPos = input.selectionEnd;
    var doc = document.selection;

    if(doc && doc.createRange().text.length != 0){
        document.getElementById(''selectedText'').value = (doc.createRange().text);
    } else if (!doc && input.value.substring(startPos,endPos).length != 0){
        document.getElementById(''selectedText'').value = (input.value.substring(startPos,endPos))  
    }
}'


MyComponent >> renderContentOn: html
    (html form)
        with: [ 
            (html hiddenInput)
                id: 'selectedText';
                callback: [ :value | selection := value ].
            (html textArea)
                callback: [ :value | theTextAreaText := value];
                id: 'myTextArea'
                with: theTextAreaText.
            (html submitButton)
                onClick: 'selectedText(myTextArea); submitForm(this)';
                with: 'Work your magic, J.S.' ].
2
On

You can do it with jQuery/Javascript. When do you want to retrieve the selected text ? What will trigger the retrieval (a user click ? Regular polling ?)