Hello all,
I’ve run into a problem with my Wikipedia Viewer. I am trying to have the user press the “Enter” key to perform a search. A Google search unfortunately did not resolve the problem for me. Can some assist on what I can do.
The CodePen link is here:
" http://codepen.io/fmunir13/pen/PGKpxA "
Many thanks!
Faheem M.
Hi, you need an event watcher, key code 13 represents enter and return
$('#searchTerm').keyup(function(event) {
var term = $('#searchTerm').val(); //empty search box
});
if (event.keyCode == 13) {
if (term !== '') {
//Do stuff
}
}
});
HTH
Mark
I think the recommended approach is to use the event.which property. One other thing to watch out for is if you have the input box in a form, the key event may be submitting the form, so you may need to preventDefault() on that event. Try:
$(’#searchTerm’).on(‘keyup’, function(event){
if( event.which === 13){
console.log( ‘search term =’, $(’#searchTerm’).val());
}
});
Thank you, thank you both! I solved my problem using what was posted.
Mnay thanks.