Reputation: 973
is it possible I could use charAt function within function get selection as below?
getSelected function () {
var userSelection;
if (window.getSelection) {
userSelection = window.getSelection ();
} else if (document.selection) {
userSelection = document.selection.createRange ();
}
userSelection return;
}
eg : i have a whole text welcome to stack over flow, then the selected text is stack. can i make a function to get specified position with charAt when user select the text and system will compare it with all the text,if system found the same text, system will return the position??
because in my knowledge charAt function can be used to determine the specific position of the string .. http://help.dottoro.com/ljetnrhv.php#charAt
Upvotes: 0
Views: 323
Reputation: 53940
not directly, because both methods return an object rather than string. To use charAt you need to obtain the string value of selection, along the lines of
function getSelectionText() {
if (window.getSelection)
return window.getSelection().toString();
if (document.selection)
return document.selection.createRange().text;
return null;
}
alert(getSelectionText().charAt(2)); // 3rd selected char
To find a position of a string inside another string use indexOf. Finding a position of the selection on a page is far more complicated because you need to take html structure into account. Start here and here to find out more.
Upvotes: 1