I have an HTML form with multiple text inputs. I want to clear the element that had the focus immediately prior to when the \'Clear\' button is pressed. How do I ge
Building my answer on Tim Down's answer, using 'pointerdown' event will work even on touchscreens.
var toBeCleared;
const btnClear = document.querySelector('#btn-clear'); // your clear button
btnClear.addEventListener('pointerdown', function(event) {
toBeCleared = document.activeElement;
});
btnClear.addEventListener('click', function(event) {
toBeCleared.value = "";
});