/    Sign up×
Community /Pin to ProfileBookmark

Selection in Javascript

text selection in input fields and textpositions

In this thread you can post all your functions and code to supprot javascript selection…

Cheers!

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@zjuupauthorJul 26.2006 — [CODE]// DES: Gets the active selected text
// RET: The selected text. EMPTY STRING when there is no selection
function getSelectedText(){
if (document.selection != undefined){
if (document.selection.type == "Text"){
return document.selection.createRange().text;
} // if
} // if
return "";
} // getSelectedText[/CODE]
Copy linkTweet thisAlerts:
@zjuupauthorJul 26.2006 — [CODE]// DES: Function retrieves the startposition of the selected text within the current parents control
// PAR: parParentControl = A reference to the control containing the selection.
// RET: The start position of the selected text.
// -1 if the selection did not exist
// -2 The parentcontrol is undefined
function getSelectedTextPosition(parParentControl){

var intResultPosition = -1;
var strSelection = getSelectedText();

if (parParentControl == undefined){
intResultPosition = -2;
// no parent control

} else if (strSelection.length > 0){

var objTextRange = document.selection.createRange();

// Overwrite the text, identifying the position of the selection
objTextRange.text = "~!@" + strSelection + "@!~";

// Get the text 'selection'
var strParentElementText = parParentControl.value;

// Restore the original text
objTextRange = document.selection.createRange();
objTextRange.text = strSelection;
intResultPosition = strParentElementText.indexOf("~!@" + strSelection + "@!~");

} // if
return intResultPosition;

} // getSelectedTextPosition


// DES: Gets the active selected text
// RET: The selected text. EMPTY STRING when there is no selection
function getSelectedText(){
if (document.selection != undefined){
if (document.selection.type == "Text"){
return document.selection.createRange().text;
} // if
} // if
return "";
} // getSelectedText[/CODE]
Copy linkTweet thisAlerts:
@KorJul 26.2006 — Don't be so impetuous. Those codes work for IE only... Wana play crossbrowser with selection? See:

http://www.quirksmode.org/js/selected.html
Copy linkTweet thisAlerts:
@zjuupauthorJul 26.2006 — Correct...

However in IE there is no souch thing as determining teh selectionstart and selection enght....

Those commands do function in firefox. However IE does not support this!

These functions work for IE....

I should have mentioned...
×

Success!

Help @zjuup spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.23,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...