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>");
}
}
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>");
}
}