Joemich Joemich - 5 months ago 14
Javascript Question

Clear input field and reset search for the new API call

So I want to make the button click to start a new API call to see if the Twitch user is online or offline, and reset the input field after each click. Seems to work but each time I need to refresh my browser once or twice in order to work, and if I click multiple times console just adds the new call under the old one... I tried to clear the search with the part that's commented our under the click function, but it does not work. Can someone please tell what am I doing wrong..?

Example users: freecodecamp, vainglory, storbeck

[Here](https://jsfiddle.net/hycg23ot/1/)'s the fiddle:

Answer
$(document).ready(function(){
    //See if our streamer is online or offline
        $('#getyourstreamer').click(function(){
            var yourstreamer = $('#yourstreamer').val();
            var yourstreamerURL = 'https://api.twitch.tv/kraken/streams/' + yourstreamer;

            $.getJSON(yourstreamerURL, function(data){

                if(data.stream === null) {
                    $('#streamerStatus').html(yourstreamer + " is currently offline");
                } else {
                    $('#streamerStatus').html(yourstreamer + " is currently online");
                }
            }); 

            $('#yourstreamer').val(''); //clear input field on reload

        });
});
  • Moved the initial variables so they are assigned after every click event.
  • Moved the .val('') so it is cleared after the API call.

PS: you have an invalid </a> in your markup.