ilyo
ilyo

Reputation: 36421

Keep input focused after clearing it's value

When I clear input input.value = ''; the input field loses focus. Is there a way to clear it and keep focus on it so the user can keep typing?

Upvotes: 1

Views: 735

Answers (1)

BrTkCa
BrTkCa

Reputation: 4783

input.focus();

var input = document.getElementById("txt");
function clicked(){
  input.value = '';
  input.focus();
}
<input type="text" id="txt" />

<button onclick="clicked()"> Clear </button>

Upvotes: 1

Related Questions