Перезаписать результат на выходе. Смотрите построчные комментарии в демо.
// Register the change event to the <form>
document.forms.main.onchange = handler;
// Pass the event object
function handler(event) {
/*
- "this" is the <form>
- `field` is a NodeList of all inputs, outputs,
buttons, etc...
*/
const field = this.elements;
/*
event.target is always the tag that the user interacted
with.
*/
const interacted = event.target;
/*
In this case event.target is <input> because the of the
"change" event and the if condition only accepts
<input> or nothing.
*/
if (interacted.id === 'input') {
// Get the value of <input>
let check = interacted.value;
/*
- Determine result from switch outcome
- Note: the value is converted to lower case. It's just
better UX not to get user fusterated over trivial
crap
- The reply is directly overwitten to the <output> so
it doesn't pile up on the <output>
*/
switch (check.toLowerCase()) {
case "rifat":
field.output.innerHTML = "Rifat is checked.";
break;
case "sakib":
field.output.innerHTML = "Sakib is checked.";
break;
default:
field.output.innerHTML = "Nothing is checked.";
break;
}
}
}
:root,
body {
font: 400 3vw/1.5 Consolas;
}
input,
button,
output {
font: inherit;
}
#output {
color: tomato
}
<form id='main'>
<input id="input" placeholder="Name" type="text">
<button id="check" type="button">Check</button><br>
<output id="output"></output>
</form>