Selecting Text Within A Div, Changing Button When Text Is Highlighted (deselect When Its Unhighlighted)
I have this script from fiddle: http://jsfiddle.net/83T7U/5/. It changes the button name from 'Reply' to 'Quote' when some text is selected within a specific DIV. However, it only
Solution 1:
The original script you have is not far off. I would suggest using
- a function that gets the selected text so that you can check the selection is non-empty
- a function that gets you the container element of the selection
- a function that checks whether an element has a particular class and
- a function that gets the nearest container of a node (or the node itself) with a particular class (below).
The resulting code is longer than what you've got but is more logical, modular and readable.
Also, it's document.getElementById()
rather than document.GetElementById()
.
Demo: http://jsfiddle.net/9zXg6/6/
Code not contained in linked answers:
functiongetSelfOrAncestorWithClass(node, theClass) {
while (node) {
if (hasClass(node, theClass)) {
return node;
} else {
node = node.parentNode;
}
}
returnnull;
}
Post a Comment for "Selecting Text Within A Div, Changing Button When Text Is Highlighted (deselect When Its Unhighlighted)"