Hello, I am trying to get the value of an input using jQuery’s .val(). But that doesn’t seem to get updated as I always get the first typed value and no matter what value I type again, I still get the results of the first value when I click “Search”. Here is the code Wikepedia Viewer
Did you get this figured out? It seems to be working when I try it.
No yet! I still need to refresh the page every time I want to do a new search. I don’t want that. I want to get results just clicking the “Search” button
Ok, I figured out the problem. You’re getting the expected input (which is all I checked because laziness), but you’re not clearing out the markup you created from your last search. You need to do something like
$("#resultsTitle").html('') // empty out the HTML in #resultsTitle
each time you want to update the search.
Where should I place it? because I’ve tried it in every pocible place, but no luck
You should clear out the last search each time the search button is clicked, and it should be done before you send the request for the next search. I would suggest a couple of changes:
-
Change the search parameters a bit. Right now you’re getting the results in arrays because the Wikimedia API is really hard to understand and use. I changed your URL to
https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch=" + key + "&format=json&callback=?"
and it gave me an array of objects instead of just some arrays. -
With a more cogent form for your data, you can change how you write out each result. Instead of having 3 different places for your data,
#resultsTitle
,#resultsInto
, and#resultsLink
, you can have just one container, like#results
. That way it’s easy to clear out old data -$('#results').html('')
. Each result can be easily written out, too.
Thank you! I will try that out. Yes, the Wikimedia API is really complicated. It took me days to figure out what does out and I am still struggling with it. Hopefully I will get the hang out of it sooner