Assistance with Wikiviewer

Assistance with Wikiviewer
0

#1

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.


#2

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


#3

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());
}
});


#4

Thank you, thank you both! I solved my problem using what was posted.

Mnay thanks.