Getting the selected text in a textArea

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Getting the selected text in a textArea

Bernat Romagosa
Hi list,

I've been wrecking my brains all morning long trying to get to reference the selected text in a textArea by using javascript or whatever means.

So long, I've found a couple of snippets that should do the job, for example:

function getFocusedTextArea() {
  var textareas = document.getElementsByTagName(“textarea”);
  for(var i=0; i lessthan textareas.length; i++) {
    var ta = textareas.item(i);
    if (window.getComputedStyle(ta, null).zIndex == 57) {
      return ta;
    }
  }
}

The problem is that I don't know how to store the value returned by getFocusedTextArea() into a variable inside a component. I'd like something similar to:

html submitButton
   onClick: (html doSomethingMagicInJavascript:
       [aString := html andSomethingElseMagic: 'getFocusedTextArea();']);
   with: 'Get the selected text magically'.

Is there such thing?

Thanks!

Bernat Romagosa.

_______________________________________________
seaside mailing list
[hidden email]
http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside
Reply | Threaded
Open this post in threaded view
|

Re: Getting the selected text in a textArea

littleSmalltalker
Bernat,
You can either use Javascript to put the selected text into some hidden element, which on callback will be available as an element value, or you can use Ajax to send the value back (instead of submitting it using an onClick event).


Regards,
Avi.

On Thu, Feb 3, 2011 at 2:00 PM, AxiNat <[hidden email]> wrote:
Hi list,

I've been wrecking my brains all morning long trying to get to reference the selected text in a textArea by using javascript or whatever means.

So long, I've found a couple of snippets that should do the job, for example:

function getFocusedTextArea() {
  var textareas = document.getElementsByTagName(“textarea”);
  for(var i=0; i lessthan textareas.length; i++) {
    var ta = textareas.item(i);
    if (window.getComputedStyle(ta, null).zIndex == 57) {
      return ta;
    }
  }
}

The problem is that I don't know how to store the value returned by getFocusedTextArea() into a variable inside a component. I'd like something similar to:

html submitButton
   onClick: (html doSomethingMagicInJavascript:
       [aString := html andSomethingElseMagic: 'getFocusedTextArea();']);
   with: 'Get the selected text magically'.

Is there such thing?

Thanks!

Bernat Romagosa.

_______________________________________________
seaside mailing list
[hidden email]
http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside



_______________________________________________
seaside mailing list
[hidden email]
http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside
Reply | Threaded
Open this post in threaded view
|

Re: Getting the selected text in a textArea

Bernat Romagosa
Hi Avi,

Thanks for answering. My problem is I don't have the first idea where to look, I can't find a single example that suits me, they're all for plain HTML...

Bernat.

2011/2/3 Avi Shefi <[hidden email]>
Bernat,
You can either use Javascript to put the selected text into some hidden element, which on callback will be available as an element value, or you can use Ajax to send the value back (instead of submitting it using an onClick event).


Regards,
Avi.

On Thu, Feb 3, 2011 at 2:00 PM, AxiNat <[hidden email]> wrote:
Hi list,

I've been wrecking my brains all morning long trying to get to reference the selected text in a textArea by using javascript or whatever means.

So long, I've found a couple of snippets that should do the job, for example:

function getFocusedTextArea() {
  var textareas = document.getElementsByTagName(“textarea”);
  for(var i=0; i lessthan textareas.length; i++) {
    var ta = textareas.item(i);
    if (window.getComputedStyle(ta, null).zIndex == 57) {
      return ta;
    }
  }
}

The problem is that I don't know how to store the value returned by getFocusedTextArea() into a variable inside a component. I'd like something similar to:

html submitButton
   onClick: (html doSomethingMagicInJavascript:
       [aString := html andSomethingElseMagic: 'getFocusedTextArea();']);
   with: 'Get the selected text magically'.

Is there such thing?

Thanks!

Bernat Romagosa.

_______________________________________________
seaside mailing list
[hidden email]
http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside



_______________________________________________
seaside mailing list
[hidden email]
http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside



_______________________________________________
seaside mailing list
[hidden email]
http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside
Reply | Threaded
Open this post in threaded view
|

Re: Getting the selected text in a textArea

littleSmalltalker
How about this:

    html hiddenInput id: 'selectedText';
        callback: [ :value | aString := value ].
    html textArea name: 'myTextArea'.
    html break.
    html submitButton
        onClick: [ 'getFocusedTextArea(); submitForm(this)' ];
        with: 'Get the selected text magically'.

Change the getFocusedTextArea Javascript function so it assigns it value to the hidden field 'selectedText'.
Don't forget to check what happens when there is no selected text (either disable the form submission, or do validation on the callback).


Regards,
Avi.


On Thu, Feb 3, 2011 at 2:22 PM, AxiNat <[hidden email]> wrote:
Hi Avi,

Thanks for answering. My problem is I don't have the first idea where to look, I can't find a single example that suits me, they're all for plain HTML...

Bernat.

2011/2/3 Avi Shefi <[hidden email]>

Bernat,
You can either use Javascript to put the selected text into some hidden element, which on callback will be available as an element value, or you can use Ajax to send the value back (instead of submitting it using an onClick event).


Regards,
Avi.

On Thu, Feb 3, 2011 at 2:00 PM, AxiNat <[hidden email]> wrote:
Hi list,

I've been wrecking my brains all morning long trying to get to reference the selected text in a textArea by using javascript or whatever means.

So long, I've found a couple of snippets that should do the job, for example:

function getFocusedTextArea() {
  var textareas = document.getElementsByTagName(“textarea”);
  for(var i=0; i lessthan textareas.length; i++) {
    var ta = textareas.item(i);
    if (window.getComputedStyle(ta, null).zIndex == 57) {
      return ta;
    }
  }
}

The problem is that I don't know how to store the value returned by getFocusedTextArea() into a variable inside a component. I'd like something similar to:

html submitButton
   onClick: (html doSomethingMagicInJavascript:
       [aString := html andSomethingElseMagic: 'getFocusedTextArea();']);
   with: 'Get the selected text magically'.

Is there such thing?

Thanks!

Bernat Romagosa.

_______________________________________________
seaside mailing list
[hidden email]
http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside



_______________________________________________
seaside mailing list
[hidden email]
http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside



_______________________________________________
seaside mailing list
[hidden email]
http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside



_______________________________________________
seaside mailing list
[hidden email]
http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside
Reply | Threaded
Open this post in threaded view
|

Re: Getting the selected text in a textArea

Bernat Romagosa
Hi,

I've been fighting with it, but no luck so far... here's my (simplified) code:


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];
with: theTextAreaText.
(html submitButton)
onClick: 'selectedText(); submitForm(this)';
with: 'Work your magic, J.S.' ].


What am I doing wrong? :(

Bernat.

2011/2/3 Avi Shefi <[hidden email]>
How about this:

    html hiddenInput id: 'selectedText';
        callback: [ :value | aString := value ].
    html textArea name: 'myTextArea'.
    html break.
    html submitButton
        onClick: [ 'getFocusedTextArea(); submitForm(this)' ];

        with: 'Get the selected text magically'.

Change the getFocusedTextArea Javascript function so it assigns it value to the hidden field 'selectedText'.
Don't forget to check what happens when there is no selected text (either disable the form submission, or do validation on the callback).


Regards,
Avi.


On Thu, Feb 3, 2011 at 2:22 PM, AxiNat <[hidden email]> wrote:
Hi Avi,

Thanks for answering. My problem is I don't have the first idea where to look, I can't find a single example that suits me, they're all for plain HTML...

Bernat.

2011/2/3 Avi Shefi <[hidden email]>

Bernat,
You can either use Javascript to put the selected text into some hidden element, which on callback will be available as an element value, or you can use Ajax to send the value back (instead of submitting it using an onClick event).


Regards,
Avi.

On Thu, Feb 3, 2011 at 2:00 PM, AxiNat <[hidden email]> wrote:
Hi list,

I've been wrecking my brains all morning long trying to get to reference the selected text in a textArea by using javascript or whatever means.

So long, I've found a couple of snippets that should do the job, for example:

function getFocusedTextArea() {
  var textareas = document.getElementsByTagName(“textarea”);
  for(var i=0; i lessthan textareas.length; i++) {
    var ta = textareas.item(i);
    if (window.getComputedStyle(ta, null).zIndex == 57) {
      return ta;
    }
  }
}

The problem is that I don't know how to store the value returned by getFocusedTextArea() into a variable inside a component. I'd like something similar to:

html submitButton
   onClick: (html doSomethingMagicInJavascript:
       [aString := html andSomethingElseMagic: 'getFocusedTextArea();']);
   with: 'Get the selected text magically'.

Is there such thing?

Thanks!

Bernat Romagosa.

_______________________________________________
seaside mailing list
[hidden email]
http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside



_______________________________________________
seaside mailing list
[hidden email]
http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside



_______________________________________________
seaside mailing list
[hidden email]
http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside



_______________________________________________
seaside mailing list
[hidden email]
http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside



_______________________________________________
seaside mailing list
[hidden email]
http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside
Reply | Threaded
Open this post in threaded view
|

Re: Getting the selected text in a textArea

Bernat Romagosa
Can you believe it? Just came back from lunch and fixed it! The JS call was missing the argument... so stupid!

onClick: 'selectedText(myTextArea); submitForm(this)';

Thanks Avi!

Bernat

2011/2/3 AxiNat <[hidden email]>
Hi,

I've been fighting with it, but no luck so far... here's my (simplified) code:


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];
with: theTextAreaText.
(html submitButton)
onClick: 'selectedText(); submitForm(this)';
with: 'Work your magic, J.S.' ].


What am I doing wrong? :(

Bernat.

2011/2/3 Avi Shefi <[hidden email]>
How about this:

    html hiddenInput id: 'selectedText';
        callback: [ :value | aString := value ].
    html textArea name: 'myTextArea'.
    html break.
    html submitButton
        onClick: [ 'getFocusedTextArea(); submitForm(this)' ];

        with: 'Get the selected text magically'.

Change the getFocusedTextArea Javascript function so it assigns it value to the hidden field 'selectedText'.
Don't forget to check what happens when there is no selected text (either disable the form submission, or do validation on the callback).


Regards,
Avi.


On Thu, Feb 3, 2011 at 2:22 PM, AxiNat <[hidden email]> wrote:
Hi Avi,

Thanks for answering. My problem is I don't have the first idea where to look, I can't find a single example that suits me, they're all for plain HTML...

Bernat.

2011/2/3 Avi Shefi <[hidden email]>

Bernat,
You can either use Javascript to put the selected text into some hidden element, which on callback will be available as an element value, or you can use Ajax to send the value back (instead of submitting it using an onClick event).


Regards,
Avi.

On Thu, Feb 3, 2011 at 2:00 PM, AxiNat <[hidden email]> wrote:
Hi list,

I've been wrecking my brains all morning long trying to get to reference the selected text in a textArea by using javascript or whatever means.

So long, I've found a couple of snippets that should do the job, for example:

function getFocusedTextArea() {
  var textareas = document.getElementsByTagName(“textarea”);
  for(var i=0; i lessthan textareas.length; i++) {
    var ta = textareas.item(i);
    if (window.getComputedStyle(ta, null).zIndex == 57) {
      return ta;
    }
  }
}

The problem is that I don't know how to store the value returned by getFocusedTextArea() into a variable inside a component. I'd like something similar to:

html submitButton
   onClick: (html doSomethingMagicInJavascript:
       [aString := html andSomethingElseMagic: 'getFocusedTextArea();']);
   with: 'Get the selected text magically'.

Is there such thing?

Thanks!

Bernat Romagosa.

_______________________________________________
seaside mailing list
[hidden email]
http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside



_______________________________________________
seaside mailing list
[hidden email]
http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside



_______________________________________________
seaside mailing list
[hidden email]
http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside



_______________________________________________
seaside mailing list
[hidden email]
http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside




_______________________________________________
seaside mailing list
[hidden email]
http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside