Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

how to get javascript to store user input from html input type "text"

I am trying to create a mad-libs valentines game for my person. I want the user (my person) to input words into a form that accepts input type "text".

From there, I want javascript to store the user's inputs in variables, which will then be combined to form a message that will display in a html div.

I understand how to make forms and how to use variables to create a mad-libs from the mad-libs challenge in treehouse.

I am stuck understanding how to get javascript to hold the value users input into a variable from html input type "text."

Can anyone help me figure this out?

2 Answers

Hi Jose!

It sounds like you want to save values in local storage.

You can write a function that executes when the user clicks the submit button (instead of submitting the form).

Then capture the text field values using document.getElementById() or document.querySelect() (or similar), then store those values in local storage for later use.

More info:

https://teamtreehouse.com/library/how-to-use-local-storage

https://www.w3schools.com/jsref/prop_win_localstorage.asp

These, however, do something similar without using local storage:

https://codepen.io/Pyremell/pen/ONgexd

https://codepen.io/navcool/pen/NAayBm

https://codepen.io/HappiestMeal/pen/GwQrjq

I hope that helps.

Please let me know if you have further questions.

Stay safe and happy coding!

Peter, Thank you so much.

After looking through all the wonderful resources you sent me, I decided to use

https://codepen.io/HappiestMeal/pen/GwQrjq

It seemed like the simplest one for me to understand how to store user inputs in javascript.

I appreciate your help. I am excited to get my valentines' day project going past this hurdle.