There should be a message for the user when they leave the input box, but instead there is error in console for ajax call. How to solve this? via /r/learnprogramming


There should be a message for the user when they leave the input box, but instead there is error in console for ajax call. How to solve this?

I am making a wiki viewer, which basically displays a list of relevant Wikipedia articles based on the user search in an input box. I have it up on codepen

The problem is when the user leaves the search box empty and presses enter or the submit/search button nothing happens to the page but the console reads the error:

 Uncaught TypeError: Cannot read property 'search' of undefined 

I tried to work around it by changing the javascript up a bit(not updated on codepen):

function findSearch(){ var userSearch=encodeURIComponent(document.getElementById("searchBox").value); console.log(userSearch); if(userSearch.length<0){ $("searchResults").html("You have to type something to find results for it, you know."); }else{ $.ajax({ url:"http://ift.tt/2v9hnmV;, dataType:"jsonp", success:function(dataObj){ console.log(dataObj); var uptoArray=dataObj.query.search; //**this is where the console reads the error** console.log() //display the search results and continued.. 

Can someone please the problem to me? What is a way I can alert the user that the input is empty while there are no errors in the console? Any help would be very appreciated. Edit: formatting

Submitted July 17, 2017 at 08:09AM by SteelApple
via reddit http://ift.tt/2uzXecs

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s