How Can I Prevent Range.selectnode() Selecting Too Much Of The Dom When Attempting To Select A Node Injected With Appendchild()?
I'm facing an issue with the combination of using appendChild() and Range.selectNode() in JavaScript. When attempting to use a range to select the newly-appended
Solution 1:
Replace your call to selectObjectText
with:
container.setSelectionRange(0, container.value.length);
The problem with textarea
elements is that they do not hold their contents in DOM nodes. The text value is a property of the element. When you call range.selectNode
, what happens is that the range is set so as to encompass the node you pass to the function and the children node of this node, but since a textarea
does not store its text in children nodes, then you select only the textarea
.
setSelectionRange
works with the value of an input element so it does not suffer from this problem. You might want to check the compatibility matrix here to check which browsers support it.
Post a Comment for "How Can I Prevent Range.selectnode() Selecting Too Much Of The Dom When Attempting To Select A Node Injected With Appendchild()?"