In javascript how to have a readonly textarea and still get onselect events?

There is no straightforward way to do this by dynamically adjusting the textarea's readonly attribute onfocus, onblur, onselect, etc. While still always receiving the onselect event.

There is no straightforward way to do this by dynamically adjusting the textarea's readonly attribute onfocus, onblur, onselect, etc. While still always receiving the onselect event. If your goal is to make sure that users cannot edit/manipulate the textarea, then I would probably just leave the textarea as non-readonly (to receive the select events) but block all user input inside of it by using preventDefault() on input events, like so: var ta = document. CreateElement('textarea'); // prevent user input ta.

AddEventListener('cut', function(e) { e.preventDefault(); }, false); ta. AddEventListener('copy', function(e) { e.preventDefault(); }, false); ta. AddEventListener('paste', function(e) { e.preventDefault(); }, false); ta.

AddEventListener('keydown', function(e) { e.preventDefault(); }, false); // listen for user selections ta. AddEventListener('select', function() { // function logic... }, false).

Thank you. I had to add the following line to your list: ta. AddEventListener('dragstart', function(e) { e.preventDefault(); }, false); That aside your trick works for me.

– Xnzo72 Sep 20 at 7:59.

You can still select text in there, and it'll be non-editable, hence readonly.

You won't get select events from it though. – Tim Down Sep 19 at 16:33 onselect does not work on divs! only on textarea and text – Xnzo72 Sep 19 at 16:36.

You could make a loop function that keeps checking for window text selection. If it detects that the length of the select text has reached some sort of limit it will execute what ever you see fit. Something like this: function getSelectedText() { var txt = ''; if (window.

GetSelection) { txt = window.getSelection(); } else if (document. GetSelection) // FireFox { txt = document.getSelection(); } else if (document. Selection) // IE 6/7 { txt = document.selection.createRange().

Text; } return txt; } function doSomething() { var txt = getSelectedText(); // Do something with txt variable, like txt. Length } setInterval('doSomething()',500); You would also need to make sure the selection is from the desired textarea. Hopefully this will point you in the right direction.

Thanks. Interesting answer. I still wonder if there is something less compicated.

– Xnzo72 Sep 19 at 16:57 I suggest adding something like a button that will fire the getSelectedText() rather than using setInterval() which will slow down your application and also the client's computer. – RHT Sep 19 at 17:00 Yes, that's true. But it should not have that big of an impact.

– José P. Airosa Sep 19 at 17:04.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions