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
john larson
16,594 PointsStill playing around trying to understand things
//I'm getting an error that says
//createNewTaskElement is not a function
//I'm trying to make it so when the button is clicked
//the function runs and prints the elements to the page
//if I leave out the button altogether
//it works as intended
var button = document.getElementsByTagName("button")[0];
button.onclick = createNewTaskElement();
var createNewTaskElement = function(){
var listItem = document.createElement("li");
var checkBox = document.createElement("input");
var label = document.createElement("label");
var editInput = document.createElement("input");
var editButton = document.createElement("button");
var deleteButton = document.createElement("button");
checkBox.type = "checkbox";
editInput.type = "text";
label.innerText = "taskString";
editButton.innerText = "Edit";
editButton.className = "edit";
deleteButton.innerText = "Delete";
deleteButton.className = "delete";
listItem.appendChild(checkBox);
listItem.appendChild(label);
listItem.appendChild(editInput);
listItem.appendChild(editButton);
listItem.appendChild(deleteButton);
document.body.appendChild(listItem);
}
4 Answers
Steven Parker
244,190 Points
The problem is that you were trying to call the function before it was defined.
Also, when you establish an event handler, you want to give it the name of your function, you don't want to invoke that function:
button.onclick = createNewTaskElement; // no parens here - that would invoke the function
The function will be invoked later, when the event occurs.
Joshua Kelly
18,830 Pointsadd ';' at the end. you are declaring a variable and making a statement so you need to end with a semi-colon.
hope that helps. if not let me know and i will look more closely
john larson
16,594 PointsI don't see where I am missing a semicolon.
Steven Parker
244,190 Points Semicolons are not required at the end of a line (but they are good practice)
Joshua Kelly
18,830 PointsFYI. You could easily use the innerHTML method to get all of this done without being so verbose.
john larson
16,594 PointsI don't doubt that there could be other ways to do this, but it's part of a bigger lesson that I am trying to understand and apply.
Joshua Kelly
18,830 Pointsthe semi colon at the very bottom to close out your function declaration as a variable. When you declare a function like that:
var \variable\ = function() {} you need to close and finish the statement with a semi-colon. Your code needs to read:
var createNewTaskElement = functin() { // code here for function // };
you are missing the semi-colon to close our your statement.
john larson
16,594 Pointsok, so anonymous functions end with a semicolon. That doesn't fix the error. Besides i that were the error that wouldn't explain why it worked before I put in the button. If I take out the button and just call it, it works. Thank you for your effort though. I just realized before I put the button in, I called the function AFTER the function. If I call the function BEFORE the function I get the error. If I put the button with the onclick...nevermind I think I got it
Steven Parker
244,190 PointsAs I said above,
Semicolons are not required at the end of a line (but they are good practice)
Ante Adamović
Front End Web Development Techdegree Student 2,508 PointsAnte Adamović
Front End Web Development Techdegree Student 2,508 PointsThis is the correct answer, but I would also like to note that it's better practice to first define a function before using it.
var myFunction = function() {} myFunction();
instead of
myFunction(); var myFunction = function() {}
Because in some cases / programming languages it won't work in the same way as it does in javascript.
john larson
16,594 Pointsjohn larson
16,594 PointsAnte, I didn't really get what you were saying, although it seems like it is probably significant.
john larson
16,594 Pointsjohn larson
16,594 PointsI also tried
after the function
and then I got the error "button.addEventListener is not a function" I know there's just little details I haven't put together. I get micro focused and lose the macro focus. And or vice versa.
Ante Adamović
Front End Web Development Techdegree Student 2,508 PointsAnte Adamović
Front End Web Development Techdegree Student 2,508 PointsJohn, just change the code that you posted from
button.onclick = createNewTaskElement();to
button.onclick = createNewTaskElement;Remove the '()' in the end.
What I was saying is that you should first define a function before calling it.
That's called function definition, a piece of code where you write what your function should do.
Then AFTER you should call the function, not before.
myFunction();That's a function call, same goes for any use of the function, such as binding it to a button.
button.onclick = myFunction;