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.

JavaScript Interactive Web Pages with JavaScript Selecting Elements and Adding Events with JavaScript Perform: Selecting Elements

how does this: checkBox.type = "checkbox" modify this: var checkBox = document.createElement("input")

I get that we are creating a new element input. But that input is not yet specified as to what type it is. it's just < input type="" >. So we need to tell it to be a check box. So it seems the way we are doing it is this: checkBox.type = checkbox. This looks similar to a question I had earlier about this syntax: "var deleteButton = taskListItem.querySelector("button.delete");" and was told the "button.delete" was referring to a button with the class delete. And that makes sense in that scenario. But in this scenario I don't see a class associated with this or even if there was how it would get into the js generated html < input type="" > . So again checkBox.type looks like syntax from accessing data from an object. But I don't recall (or my mind was to glazed over from this course) hearing any discussion in the video about this syntax and how it works or where it came from. Insights welcome

1 Answer

Benjamin Barslev Nielsen
Benjamin Barslev Nielsen
18,958 Points
var checkBox = document.createElement("input");
checkBox.type = "checkbox";

It is correct that the first line creates an empty input element: < input type="" > and the variable checkBox now refers to that element. We used the class to get the delete button, because we needed to select it from the DOM, but we have the reference for free when we create the element. Therefore we can work directly with the InputElement and therefore when we write: checkBox.type, we are referring to the type attribute of the input element. Therefore when we write

checkBox.type = "checkbox";

we modify the type attribute of the input element, such that it becomes:

< input type="checkbox">

Hope this helps.

immensely, thank you