Skip to content

Commit f47537d

Browse files
committed
Add text insertion at caret demo.
1 parent 3c4da18 commit f47537d

3 files changed

Lines changed: 106 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ with.
1010

1111
## My JavaScript Demos - I Love JavaScript!
1212

13+
* [Inserting Text At The Last Known Selection / Caret Location In JavaScript](https://bennadel.github.io/JavaScript-Demos/demos/insert-text-selection/)
1314
* [Rending Emoji Glyphs Using Hexadecimal CodePoints In JavaScript](https://bennadel.github.io/JavaScript-Demos/demos/emoji-glyph-from-hex/)
1415
* [Animating The Content Property Using CSS Keyframes Animation](https://bennadel.github.io/JavaScript-Demos/demos/animate-content-css/)
1516
* [TimeMachine-Inspired Progress Indicator Using CSS Keyframes Animation](https://bennadel.github.io/JavaScript-Demos/demos/timemachine-bar-css/)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
body {
3+
font-family: sans-serif ;
4+
font-size: 18px ;
5+
}
6+
7+
#input {
8+
box-sizing: border-box ;
9+
font-size: 100% ;
10+
height: 150px ;
11+
padding: 10px ;
12+
width: 650px ;
13+
}
14+
15+
#buttons {
16+
align-items: center ;
17+
display: flex ;
18+
margin-top: 10px ;
19+
}
20+
21+
#buttons button {
22+
background-color: transparent ;
23+
border: none ;
24+
cursor: pointer ;
25+
font-family: monospace ;
26+
font-size: 100% ;
27+
margin: 2px 5px 2px 5px ;
28+
padding: 0px 0px 0px 0px ;
29+
}
30+
31+
#buttons button:focus {
32+
outline: 3px solid blue ;
33+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<title>
6+
Inserting Text At The Last Known Selection / Caret Location In JavaScript
7+
</title>
8+
9+
<link rel="stylesheet" type="text/css" href="./demo.css" />
10+
</head>
11+
<body>
12+
13+
<h1>
14+
Inserting Text At The Last Known Selection / Caret Location In JavaScript
15+
</h1>
16+
17+
<textarea id="input">Me? ... I'm scared of everything. I'm scared of what I saw, of what I did, of who I am. And most of all, I'm scared of walking out of this room and never feeling the rest of my whole life ... the way I feel when I'm with you.";</textarea>
18+
19+
<div id="buttons">
20+
<button>(Meep)</button>
21+
<button>(Moop)</button>
22+
<button>&#x1F642;</button><!-- slightly smiling face -->
23+
<button>&#x2639;&#xFE0F;</button><!-- frowning face -->
24+
</div>
25+
26+
<script type="text/javascript" src="../../vendor/jquery/3.6.0/jquery-3.6.0.min.js"></script>
27+
<script type="text/javascript">
28+
29+
var input = $( "#input" );
30+
var buttons = $( "#buttons" );
31+
32+
// When the user clicks on one of the target text-tokens, we are going to insert
33+
// that token into the textarea at the last known selection location.
34+
buttons.on( "click", "button", handleButtonClick );
35+
36+
function handleButtonClick() {
37+
38+
var inputValue = input.val();
39+
// For the sake of simplicity, we're going to pull the insertable text right
40+
// out of the DOM structure. Fun fact, using .textContent will pull the emoji
41+
// glyph that was generated by HTML entities.
42+
var insertToken = $( this ).text();
43+
44+
// NOTE: Even after the input is blurred, it appears to retain its last-known
45+
// selection properties. As such, we don't have to track those through the
46+
// life-cycle of the page - we can just reference them at any time.
47+
var insertTokenAt = input.prop( "selectionEnd" );
48+
// After we insert the text, we're going to want to re-focus the input.
49+
// However, we're going to want to advance the selection such that it starts
50+
// just after the inserted text.
51+
var nextSelectionEnd = ( insertTokenAt + insertToken.length );
52+
53+
// Insert the text at the given location within the input.
54+
input.val(
55+
inputValue.slice( 0, insertTokenAt ) +
56+
insertToken +
57+
inputValue.slice( insertTokenAt )
58+
);
59+
60+
// Advance the text selection to just after the inserted text.
61+
input
62+
.prop( "selectionStart", nextSelectionEnd )
63+
.prop( "selectionEnd", nextSelectionEnd )
64+
.focus()
65+
;
66+
67+
}
68+
69+
</script>
70+
71+
</body>
72+
</html>

0 commit comments

Comments
 (0)