So I’m working on my Wikipedia Viewer and I’m having a lot of trouble using jQuery to pass information from my search bar into my ajax search. I think it’s something to do with the scope of my variables, because the program works when I get rid of the click event altogether and hard code my “searchTerm” variable to look up a specific term.
Hopefully that makes sense! It’s not styled at all, but here’s what I’ve got so far:
Thanks for taking a look!
I think what’s happening is that when you click your submit button, it tries to actually “submit” data like forms do normally.
You could do one of two things to fix this:
- Change your input submit button to just a normal button.
Right now you have
<input type="submit" id="search-value" value="Search Wikipedia">
You could change this to
<button type="button" id="search-value">Search Wikipedia</button>
- You could add an
event parameter to your click handler, then call
event.preventDefault() to prevent the submit button’s default behavior of submitting data (and also causing the page to navigate, or in this case just kind of reload).
Let me know if you run into any more problems!
Wow, thank you, using the the event.preventDefault() parameter worked! I’m facepalming here because it’s such a straightforward answer. I definitely still have a lot to learn!
Thanks for taking the time to look over my code – you saved me hours of frustration.