File tree Expand file tree Collapse file tree 3 files changed +83
-17
lines changed
files/en-us/web/api/caretposition Expand file tree Collapse file tree 3 files changed +83
-17
lines changed Original file line number Diff line number Diff line change @@ -32,13 +32,24 @@ A {{domxref("DOMRect")}} object.
3232<input aria-label =" text field" value =" Click inside this input field" />
3333```
3434
35- ``` css
35+ ``` html hidden
36+ <pre id =" log" ></pre >
37+ ```
38+
39+ ``` css hidden
3640input {
3741 width : 100% ;
3842 padding : 10px ;
3943 font-size : 16px ;
4044 box-sizing : border-box ;
4145}
46+
47+ #log {
48+ height : 200px ;
49+ overflow : scroll ;
50+ padding : 0.5rem ;
51+ border : 1px solid black ;
52+ }
4253```
4354
4455``` js
@@ -47,16 +58,27 @@ document.querySelector("input").addEventListener("click", (event) => {
4758 const y = event .clientY ;
4859
4960 const caret = document .caretPositionFromPoint ? .(x, y);
50- if (! caret) return ;
61+ if (! caret) {
62+ log (" Not supported" );
63+ return ;
64+ }
5165
5266 const rect = caret .getClientRect ();
5367
54- console . dir ( " Caret bounding rect:" , rect);
55- console . log (` Caret is at (${ rect .x .toFixed (2 )} , ${ rect .y .toFixed (2 )} )` );
68+ log ( ` Caret bounding rect: ${ JSON . stringify ( rect) } ` );
69+ log (` Caret is at (${ rect .x .toFixed (2 )} , ${ rect .y .toFixed (2 )} )` );
5670});
5771` ` `
5872
59- {{EmbedLiveSample("get_client_rect", "", 300)}}
73+ ` ` ` js hidden
74+ const logElement = document .querySelector (" #log" );
75+ function log (text ) {
76+ logElement .innerText = ` ${ logElement .innerText }${ text} \n ` ;
77+ logElement .scrollTop = logElement .scrollHeight ;
78+ }
79+ ` ` `
80+
81+ {{EmbedLiveSample("Get the caret's screen position", "", 300)}}
6082
6183## Specifications
6284
Original file line number Diff line number Diff line change @@ -18,19 +18,30 @@ An integer.
1818
1919## Examples
2020
21- This example logs the ` offsetNode ` and ` offset ` of the caret position when clicking inside the input field
21+ This example logs the ` offsetNode ` and ` offset ` of the caret position when clicking inside the input field.
2222
2323``` html
2424<input aria-label =" text field" value =" Click inside this input field" />
2525```
2626
27- ``` css
27+ ``` html hidden
28+ <pre id =" log" ></pre >
29+ ```
30+
31+ ``` css hidden
2832input {
2933 width : 100% ;
3034 padding : 10px ;
3135 font-size : 16px ;
3236 box-sizing : border-box ;
3337}
38+
39+ #log {
40+ height : 200px ;
41+ overflow : scroll ;
42+ padding : 0.5rem ;
43+ border : 1px solid black ;
44+ }
3445```
3546
3647``` js
@@ -39,17 +50,28 @@ document.querySelector("input").addEventListener("click", (event) => {
3950 const y = event .clientY ;
4051
4152 const caret = document .caretPositionFromPoint ? .(x, y);
42- if (! caret) return ;
53+ if (! caret) {
54+ log (" Not supported" );
55+ return ;
56+ }
4357
4458 const node = caret .offsetNode ;
4559 const offset = caret .offset ;
4660
47- console . log (" offsetNode:" , node);
48- console . log (" offset:" , offset);
61+ log (` offsetNode: ${ node} ` );
62+ log (` offset: ${ offset} ` );
4963});
5064` ` `
5165
52- {{EmbedLiveSample("offset", "", 300)}}
66+ ` ` ` js hidden
67+ const logElement = document .querySelector (" #log" );
68+ function log (text ) {
69+ logElement .innerText = ` ${ logElement .innerText }${ text} \n ` ;
70+ logElement .scrollTop = logElement .scrollHeight ;
71+ }
72+ ` ` `
73+
74+ {{EmbedLiveSample("Examples", "", 300)}}
5375
5476## Specifications
5577
Original file line number Diff line number Diff line change @@ -16,19 +16,30 @@ A {{domxref("Node")}}.
1616
1717## Examples
1818
19- This example logs the ` offsetNode ` and ` offset ` of the caret position when clicking inside the input field
19+ This example logs the ` offsetNode ` and ` offset ` of the caret position when clicking inside the input field.
2020
2121``` html
2222<input aria-label =" text field" value =" Click inside this input field" />
2323```
2424
25- ``` css
25+ ``` html hidden
26+ <pre id =" log" ></pre >
27+ ```
28+
29+ ``` css hidden
2630input {
2731 width : 100% ;
2832 padding : 10px ;
2933 font-size : 16px ;
3034 box-sizing : border-box ;
3135}
36+
37+ #log {
38+ height : 200px ;
39+ overflow : scroll ;
40+ padding : 0.5rem ;
41+ border : 1px solid black ;
42+ }
3243```
3344
3445``` js
@@ -37,17 +48,28 @@ document.querySelector("input").addEventListener("click", (event) => {
3748 const y = event .clientY ;
3849
3950 const caret = document .caretPositionFromPoint ? .(x, y);
40- if (! caret) return ;
51+ if (! caret) {
52+ log (" Not supported" );
53+ return ;
54+ }
4155
4256 const node = caret .offsetNode ;
4357 const offset = caret .offset ;
4458
45- console . log (" offsetNode:" , node);
46- console . log (" offset:" , offset);
59+ log (` offsetNode: ${ node} ` );
60+ log (` offset: ${ offset} ` );
4761});
4862` ` `
4963
50- {{EmbedLiveSample("offsetnode", "", 300)}}
64+ ` ` ` js hidden
65+ const logElement = document .querySelector (" #log" );
66+ function log (text ) {
67+ logElement .innerText = ` ${ logElement .innerText }${ text} \n ` ;
68+ logElement .scrollTop = logElement .scrollHeight ;
69+ }
70+ ` ` `
71+
72+ {{EmbedLiveSample("Examples", "", 300)}}
5173
5274## Specifications
5375
You can’t perform that action at this time.
0 commit comments