Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.


Storing HTML Value in a variable

I am working on a To Do list application. The outcome I am hoping for is when the add button is clicked it will store the value of the task input box within a variable.

You can view the codepen here: http://codepen.io/bluehabit/pen/YwWdKX, but the code that I am specifically referring to is this portion below.

var taskInput = document.getElementById('new-task'); 

var addTask = function () {
    var holder = taskInput.value;

For example if I entered 'go shopping' into the task input box, and then clicked the add button, the new value for the holder variable should be 'go shopping'.

That's only for JQuery Jeremy, .value works with default JavaScript which he seems to be using here. I do recommend JQuery though, it makes this stuff a lot easier in my opinion.

2 Answers

Doesn't look like JavaScript knows when the button is being pressed. Try adding an onclick event to your button.

<button onclick="addTask()">Add</button>

It should look towards the bottom of the JS file.

addButton.onclick = addTask;

Oh okay. Well, addButton doesn't exist, at least in the pen you linked here. You would need to add an addButton id to the button element, and then write the onclick event like this -

document.getElementById("addButton").onclick = addTask;

Shouldn't you be doing this with PHP? and ajax to make it more user friendly