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

Store response from input.

Hello,

I am trying to create a quiz. For each question I have added a text input, so that the user can write their answer. How can I get that answer and store it in a variable for use later on?

I have tried the following:

var answerOne = document.getElementById("answer-one").value;

This did not work. The console responded with the message "Cannot read property 'value' of null". My code needs improving, how should I approach this to write DRY code.

Thanks!

1 Answer

That error message likely means there is nothing on your HTML page with the id of answer-one. The getElementById() will return null because it can't find anything, and null has no properties, hence the error message.