Manipulating selected text in a TEXTAREA...

D

Daniel Pitts

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.startPost,
selectionManipulator.endPos);
},
replaceSelection: function(value) {
component.value = component.value.substring(0,
selectionManipulator.startPost) + value +

component.value.substring(selectionManipulator.endPos,
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>");
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top