By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,461 Members | 1,108 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,461 IT Pros & Developers. It's quick & easy.

Manipulating selected text in a TEXTAREA...

P: n/a
Why doesn't this work?
I create an object which is supposed to handle the selection in both IE
and Firefox, but everytime I call getText() in firefox, I get the whole
textarea, not just the selected text. Am I doing something really
stupid? Also, all I really want to do is implement the function
addParagraphTags(component) (and other similar functions), if there is
an easier way to do these that is cross-compatible, please let me know.

--- edit-functions.js --
function getSelectionManipulator(component) {
component.focus();
if (component.createTextRange) {
selectionManipulator = {
textRange: document.selection.createRange().duplicate(),
getText: function() {
return selectionManipulator.textRange.text;
},
replaceSelection: function(value) {
selectionManipulator.textRange.text = value;
}

}
} else {
selectionManipulator = {
startPos: component.selectionStart,
endPos: component.selectionEnd,
getText: function() {
return
component.value.substring(selectionManipulator.sta rtPost,
selectionManipulator.endPos);
},
replaceSelection: function(value) {
component.value = component.value.substring(0,
selectionManipulator.startPost) + value +

component.value.substring(selectionManipulator.end Pos,
component.value.length);
}
}
}
selectionManipulator.surroundSelection = function(prepend, append)
{
selectionManipulator.replaceSelection(prepend +
selectionManipulator.getText() + append);
};
selectionManipulator.isAllSpace =
/^\s*$/.test(selectionManipulator.getText());
return selectionManipulator;
}

function addParagraphTags(component) {
selectionManipulator = getSelectionManipulator(component);
if (selectionManipulator.isAllSpace) {
selectionManipulator.replaceSelection("</p><p>");
} else {
selectionManipulator.surroundSelection("<p>", "</p>");
}
}
------------------------

Thanks for the help.
Daniel.

Nov 21 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Daniel Pitts escreveu:
Why doesn't this work?
I create an object which is supposed to handle the selection in both IE
and Firefox, but everytime I call getText() in firefox, I get the whole
textarea, not just the selected text. Am I doing something really
stupid?
Take a look here: <URL:http://jsfromhell.com/forms/selection>.
--
Jonas Raoni Soares Silva
http://www.jsfromhell.com
Nov 21 '06 #2

P: n/a

Daniel Pitts wrote:
Why doesn't this work?

Ugh... I had a typo, thats why it didn't work. Ohwell. Thanks for the
lack of warnings, JavaScript!

Nov 21 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.