I have text input item and using select() I can highlight the text inside it, however sometimes the text is already selected for a valid reasons and when I run select(), it deselects it I think, is it possible to check if the input item is selected ?
+1
A:
You can check for a selection using http://www.codetoad.com/javascript_get_selected_text.asp.
You will have to check if the selection is in your input item.
var selecttxt = '';
if (window.getSelection) {
selecttxt = window.getSelection();
} else if (document.getSelection) {
selecttxt = document.getSelection();
} else if (document.selection) {
selecttxt = document.selection.createRange().text;
}
var textofinput = [...]; // put code to find text in input here
if (textofinput.indexOf(selecttxt) !== -1) {
// part of the text in the input is selected
alert('Conditions met!');
}
MvanGeest
2010-06-15 15:32:25
This is problematic if you might also have the same string selected in a different text box. For example, if you have two fields and both contain the string "hello", and the text is selected in one of them, this kind of check will not be able to tell which. I'm not expert enough to know if there's a cross-browser way to make that distinction, though.
Walter Mundt
2010-06-15 15:48:04
Agreed, but I don't know what the user's further requirements are. This might work fine, in which case it's not a big problem.
MvanGeest
2010-06-15 15:57:48
It's a grid so it will be an issue since I need to know which Input cell i'm in.
Rubans
2010-06-16 09:10:49