I’m working on my Wikipedia viewer and I think it’s working pretty well… but I have a question about forms. I have an input and a button to call the javascript function getArticles that I defined. It works great, but when I enclose the input and button in a form element, it doesn’t call the function anymore. Any idea why that is?
Any other input on how I can improve my code is also welcome. Thanks!
Hey I looked through your JS, and gave some edits that may help if you’d like to use a form.
$("#submit").on('click', function(e){
e.preventDefault();
var txt = $("#search").val();
getArticles(txt);
});
$("#search").keyup(function(event){
if(event.keyCode == 13){
var txt = $("#search").val();
getArticles(txt);
}
});
You could replace the code at the top of your JS with that. I believe it had to do with the variable txt being inside the function, and also not being able to prevent the page from reloading when clicking or submitting with enter. You could also write a function to get the query value instead of the ugly way I wrote it here