|
| 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>🙂</button><!-- slightly smiling face --> |
| 23 | + <button>☹️</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