Skip to content Skip to sidebar Skip to footer

Trigger Event Based On Specific Inputs Into A Textarea

I'm trying to trigger a JS event when there are four consecutive linebreaks inside of a textarea. The situation causing this would either be that a person presses the RETURN key fo

Solution 1:

Try below solution:

document.getElementById("textArea").addEventListener("keyup", function(event) {
  var numberOfLineBreaks = (this.value.match(/\n/g)||[]).length;
  if (numberOfLineBreaks === 4) {
    console.log("4 consecutive Lines!");
  }
});

Solution 2:

Check that easy solution:

document.getElementById("myInput").addEventListener("input", function(event) {
  if (this.value.includes('\n\n\n\n')) {
    console.log("4 consecutive return!");
  }
});
#myInput {
  width: 90%;
  height: 100px;
}
<textareaid="myInput"></textarea>

You're lucky that I was interested in the subject, and that it's really easy to do! I'm pretty sure you can do it by yourself next time.

Post a Comment for "Trigger Event Based On Specific Inputs Into A Textarea"