Search code examples
javascriptjqueryfirebasecodemirrorfirepad

Get Firepad textarea element or current character while typing


I'm trying to implement a hashtag like behaviour in Firepad using JQuery.

Is there any way of getting the textarea element or the character you are currently typing in the Firepad editor in order to, for example, when i type '#' trigger an event?

I have tried to use the getText() function, but it only gives me the whole string when it is synced with Firebase so when i'm fast typing it is not synced until i stop for a second so i can't trigger an event in the exact moment i type a specific character.


Solution

  • I don't know about FirePad, but with Textarea you can do something like this.

    document.querySelector("textarea").addEventListener("keydown", (event) => {
      if (event.key === "#") window.alert("# found");
    }, false);
    
    document.querySelector("div").addEventListener("keydown", (event) => {
      if (event.key === "#") window.alert("# found");
    }, false);
    div {
      border: 1px solid #aaa;
    }
    <textarea></textarea>
    <div contenteditable="true"></div>