|
8 | 8 |
|
9 | 9 | In this chapter we'll cover selection in the document, as well as selection in form fields, such as `<input>`.
|
10 | 10 |
|
11 |
| -JavaScript can do get the existing selection, select/deselect both as a whole or partially, remove the selected part from the document, wrap it into a tag, and so on. |
| 11 | +JavaScript can get the existing selection, select/deselect both as a whole or partially, remove the selected part from the document, wrap it into a tag, and so on. |
12 | 12 |
|
13 | 13 | You can get ready to use recipes at the end, in "Summary" section. But you'll get much more if you read the whole chapter. The underlying `Range` and `Selection` objects are easy to grasp, and then you'll need no recipes to make them do what you want.
|
14 | 14 |
|
15 | 15 | ## Range
|
16 | 16 |
|
17 | 17 | The basic concept of selection is [Range](https://dom.spec.whatwg.org/#ranges): basically, a pair of "boundary points": range start and range end.
|
18 | 18 |
|
19 |
| -Each point represented as a parent DOM node with the relative offset from its start. If the parent node is an element element node, then the offset is a child number, for a text node it's the position in the text. Examples to follow. |
| 19 | +Each point represented as a parent DOM node with the relative offset from its start. If the parent node is an element node, then the offset is a child number, for a text node it's the position in the text. Examples to follow. |
20 | 20 |
|
21 | 21 | Let's select something.
|
22 | 22 |
|
@@ -95,8 +95,8 @@ Let's select `"Example: <i>italic</i>"`. That's two first children of `<p>` (cou
|
95 | 95 | </script>
|
96 | 96 | ```
|
97 | 97 |
|
98 |
| -- `range.setStart(p, 0)` -- sets the start at the 0th child of `<p>` (that's a text node `"Example: "`). |
99 |
| -- `range.setEnd(p, 2)` -- spans the range up to (but not including) 2nd child of `<p>` (that's a text node `" and "`, but as the end is not included, so the last selected node is `<i>`). |
| 98 | +- `range.setStart(p, 0)` -- sets the start at the 0th child of `<p>` (that's the text node `"Example: "`). |
| 99 | +- `range.setEnd(p, 2)` -- spans the range up to (but not including) 2nd child of `<p>` (that's the text node `" and "`, but as the end is not included, so the last selected node is `<i>`). |
100 | 100 |
|
101 | 101 | Here's a more flexible test stand where you try more variants:
|
102 | 102 |
|
@@ -387,7 +387,7 @@ Also, there are convenience methods to manipulate the selection range directly,
|
387 | 387 | - `setBaseAndExtent(anchorNode, anchorOffset, focusNode, focusOffset)` - replace selection range with the given start `anchorNode/anchorOffset` and end `focusNode/focusOffset`. All content in-between them is selected.
|
388 | 388 | - `selectAllChildren(node)` -- select all children of the `node`.
|
389 | 389 | - `deleteFromDocument()` -- remove selected content from the document.
|
390 |
| -- `containsNode(node, allowPartialContainment = false)` -- checks whether the selection contains `node` (partically if the second argument is `true`) |
| 390 | +- `containsNode(node, allowPartialContainment = false)` -- checks whether the selection contains `node` (partially if the second argument is `true`) |
391 | 391 |
|
392 | 392 | So, for many tasks we can call `Selection` methods, no need to access the underlying `Range` object.
|
393 | 393 |
|
@@ -551,7 +551,7 @@ If nothing is selected, or we use equal `start` and `end` in `setRangeText`, the
|
551 | 551 |
|
552 | 552 | We can also insert something "at the cursor" using `setRangeText`.
|
553 | 553 |
|
554 |
| -Here's an button that inserts `"HELLO"` at the cursor position and puts the cursor immediately after it. If the selection is not empty, then it gets replaced (we can do detect in by comparing `selectionStart!=selectionEnd` and do something else instead): |
| 554 | +Here's a button that inserts `"HELLO"` at the cursor position and puts the cursor immediately after it. If the selection is not empty, then it gets replaced (we can detect it by comparing `selectionStart!=selectionEnd` and do something else instead): |
555 | 555 |
|
556 | 556 | ```html run autorun
|
557 | 557 | <input id="input" style="width:200px" value="Text Text Text Text Text">
|
@@ -583,7 +583,7 @@ To make something unselectable, there are three ways:
|
583 | 583 |
|
584 | 584 | This doesn't allow the selection to start at `elem`. But the user may start the selection elsewhere and include `elem` into it.
|
585 | 585 |
|
586 |
| - Then `elem` will become a part of `document.getSelection()`, so the selection actully happens, but its content is usually ignored in copy-paste. |
| 586 | + Then `elem` will become a part of `document.getSelection()`, so the selection actually happens, but its content is usually ignored in copy-paste. |
587 | 587 |
|
588 | 588 |
|
589 | 589 | 2. Prevent default action in `onselectstart` or `mousedown` events.
|
@@ -632,7 +632,7 @@ The most used recipes are probably:
|
632 | 632 | cloned.append(selection.getRangeAt(i).cloneContents());
|
633 | 633 | }
|
634 | 634 | ```
|
635 |
| -2. Setting the selection |
| 635 | +2. Setting the selection: |
636 | 636 | ```js run
|
637 | 637 | let selection = document.getSelection();
|
638 | 638 |
|
|
0 commit comments