Skip to content Skip to sidebar Skip to footer

How To Continually Add Typed Characters To A Variable?

I need to know how to add the character a user pressed to a variable without erasing the variables previous data. For example, when a user types a it adds a to a variable. Then the

Solution 1:

You can utilize oninput event, create a variable that is undefined, at first input event, set window[variable] and created variable to character user input; at subsequent input from user, concatenate window[variable] from last character at user input using String.prototype.slice() with parameter -1

var q = void 0;

document.querySelector("input").oninput = function(e) {
  if (!q && !window[e.target.value]) {
    window[e.target.value] = q = e.target.value;
  } else {
    window[q] += e.target.value.slice(-1);
    console.log(window[q])
  }
}
<input type="text" />

Post a Comment for "How To Continually Add Typed Characters To A Variable?"