funkyeah
funkyeah

Reputation: 3194

Live pattern matching in a textarea to trigger events

If you had a textarea, and you wanted to execute a javascript function after a pre-determined 2 character string was entered, how might you go about monitoring for that string?

A caveat is that the 2 character string might not be entered on sequential key presses.

Example:

You want to trigger an event after the 2 character string "<<" is entered. So you are typing and you enter "<", then you click somewhere else in the textarea and change some text, then come back to where you left off and put a second "<" character next to the first. The fact that there are two adjacent "<<" characters should trigger an event that can be capture by javascript. How would you monitor for and create that event?

Upvotes: 0

Views: 232

Answers (1)

Explosion Pills
Explosion Pills

Reputation: 191789

You can check the entire textarea content onkeyup. I doubt that the textarea's content will become so big so as to make it prohibitive.

document.getElementById('textarea-id').addEventListener('keyup', function () {
    this.value = this.value.replace('>>', '<<');
});

Upvotes: 1

Related Questions