Is there a way to trigger the hitting of "Esc" key as well when clicking the <input type="reset">
button? ( For some reason, the existing function needs Esc to work properly. )
To be precise, the desired result is:
- Typing in the input field
- Click "Reset"
- Clear input + Hitting the "Esc" key
Here is the form created for example:
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js"></script>
<form onsubmit="event.preventDefault(); myFunction();">
<input type="text" id="quick-search" required="" />
<input type="reset" id="reset-btn" value="Reset" />
</form>
<style>
input#quick-search:not(:valid)~input#reset-btn {
display: none;
}
</style>
<script>
$(document).keydown(function(e) {
if (e.key === "Escape") {
$("#reset-btn").click();
}
});
</script>
I know how to make "Esc" as clicking reset
, but cannot find the answer to make clicking reset
also comes with a hit on "Esc".
2
Answers
Thanks for the comment on the question.
The script turns out be like this:
Ref: https://stackoverflow.com/a/55102965/1468253