I am working with a reddit API. I have a search box on my page and I am trying to set it up so that if one were to type virtually anything it would return the top 10 news stories related to the word or phrase they typed.
<script>
$(document).ready(function () {
//reddit API URL
//var URL = "http://www.reddit.com/r/subreddits/search.json?q=Xbox";
var SEARCH_URL = 'http://www.reddit.com/r/subreddits/search.json?jsonp=?';
var searchQueryText = $('#search').val(); //getSearchQueryText();
$.getJSON(SEARCH_URL, {
q: searchQueryText,
limit: 10
}).done(function (data) {
$.each(data.data.children, function (i, item) {
$("<p>").text(item.data.title).appendTo("#images");
});
}).fail(function (data) {
alert("Something went wrong");
});
}); //end ready
</script>
<div id="images">
<h1>Reddit Data:</h1>
<p>
<label>Search</label>
<input type = "text" id = "search" value = "" />
</p>
If I change the var SearchQueryText
to a specific word than the API works fine. However since I changed it to the $('#search').val();
it no longer works. I put an alert()
to test for the value and the pop-up is blank. So I think the problem is it doesn't detect the word when I type it. So I wrote a simple .mouseenter
function and still nothing. Any ideas?