Problems with storing the input on a field into an array using JS via /r/learnprogramming


Problems with storing the input on a field into an array using JS

Hey everyone, I've been working really hard to build this site that I am working on and I have come a long way as far as functionality goes(I'll post the link in a later post when I feel it is ready for more criticism. But for now, the issue I am having is I have an input field on my webpage that is supposed to be linked with my javascript page to save the user's input into an array. I have constructed a function to do this task.

I have the function working somewhat properly. If the user submits an empty field, I have it coded to pop-up an alert telling them to fill-out the form. But once they submit their information, I have no idea where it is stored, or how to access it outside of my function(the console shows it as being created, so I feel like I am having an issue with scope). If anyone could help steer me in the right direction, that would be awesome!

I am not 100% sure on the guidlines for posting larger sections of code here(as I will be sharing the html and js code), so I am going to type the relevent section of the html code below, as well as the JS, and I will post a link to the JS Bin page where I have my code stored. Thank you in advance!

 <form class="centered" onsubmit="checkSuggestionBox(suggestion);"> Suggest a song for your mood!: <input type="text" id="suggestion" size="35" onfocus="this.style.backgroundColor='LightYellow';" onblur="this.style.backgroundColor='LightBlue';"> <input type="submit" value="Submit"> </form> var suggestion = []; suggestion = console.log(checkSuggestionBox(suggestion)); //This makes an alert appear when user submits blank form function checkSuggestionBox(Id) { if (document.getElementById("suggestion").value === "") { alert("Help us help you, leave a suggestion in the box!"); //This is supposed to collect the user data and store it into an array } else if (document.getElementById("suggestion").value !== "") { var suggestion = []; var x = suggestion[0]; suggestion[x] = suggestion.push(document.getElementById("suggestion").value); console.log(suggestion); } } 

Submitted July 17, 2017 at 10:12PM by c19890611
via reddit http://ift.tt/2tzWUWs

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