On Text correction, trigger event. Possible?

Suppose I have two textarea html elements and what I type in one shows in the other.

Is there a javascript event that will fire when the correct word is selected on the screenshot below?

image1

Answer

There’s no way to get this from the event. You can write your own ‘logic’ to detect any words changes by checking the change event.

Small example using the split() and filter() functions to check the changed word:

function inputChange(event, oldvalue) {

    // Old words
    const old = oldvalue.split(' ');
    
    // Current words
    const curr = event.target.value.split(' ');
    
    // Changed words
    const diff = old.filter(x => !curr.includes(x));
    
    // For each changed word
    for (let changeId in diff) {
    
      // Diff
      console.log('Word changed: ', diff[changeId]);
      
      // Optionally, you can get the same index, so you'll know the new value
      const ind = old.indexOf(diff[changeId]);
      console.log('New value: ', curr[ind]);
    } 
}
input { width: 300px; }
<input type='textarea' value='Test txt. Correct this: hoube -> house' onfocus="this.oldvalue = this.value;" onchange="inputChange(event, oldvalue)" />

Leave a Reply

Your email address will not be published. Required fields are marked *