Wikipedia Viewer, having issue with keydown event

Wikipedia Viewer, having issue with keydown event
0

#1

Hello all.
After many hours of frustration and trying with and without form tags and various other things discussed on this forum and on stack overflow, I can’t seem to get the return key to trigger the #getJSON call. I have used alert(“This is working”); at various points in my code and it seems to be breaking at the AJAX call. I built this originally locally with the JS console and there is no bug. The button works great. There is no AJAX error message either.

I’ve also googled this and can’t get advice I’ve found to work. According to the jQuery documentation, the #keyup should work fine with forms so I kept it in my HTML. I’m really stumped why this isn’t working. Any help is much appreciated!

Here is a link to the CodePen: http://codepen.io/testinnplayin/pen/LRNjwd

Edit - if I remove the form tags the entire thing breaks, even the button.


#2

If I’m correct, pressing Enter while the focus is on a text box in a form somehow submits the data in that form. If the <form> has no action attribute it will just cause the page to reload.

Add e.preventDefault() to the first line in the if-block in the keydown event.


#3

Oh my, I am going to crawl into a corner. I did have e.preventDefault but dropped the ()! It works now, thank you so much! :blush: