I have been trying to disable the search on enter key press function within Zendesk Guide but nothing seems to take effect in my theme scripts, any suggestions?
1.
// Submit search on input enter
$quickSearch.on('keypress', (e) => {
if (e.which === 13) {
search();
}
});
});
2.
const searchBox = document.querySelector("query"); searchBox.addEventListener("keydown", (event) => { if (event.keyCode === 13) { event.preventDefault(); }});*/
3.
$( document ).ready(function() {
$(window).keydown(function(event){
if(event.keyCode == 13) {
event.preventDefault();
return false;
}
});
You need to make sure your event listener is called before any other event listener. Try to use a global event listener and set the useCapture parameter of addEventListener()
to true
, then call the stopImmediatePropagation()
method.
Something like this should work:
$(document).ready(function() {
document.addEventListener('keydown', (e)=>{
if (e.which == 13 && e.target.id == "query") {
e.preventDefault()
e.stopImmediatePropagation()
//do something
}
}, true)
})