Joemich Joemich - 1 year ago 92
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]('s the fiddle:

Answer Source
    //See if our streamer is online or offline
            var yourstreamer = $('#yourstreamer').val();
            var yourstreamerURL = '' + yourstreamer;

            $.getJSON(yourstreamerURL, function(data){

                if( === 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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download