Search code examples
smalltalkseaside

Referencing the selected text in a textarea


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


Solution

  • 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.' ].