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 trialjohn larson
16,594 PointsI'm thinking we could have used addClass just as well? But I don't really know.
//instead of building up the html this way
for(var i = 0; i < employees.length; i += 1){
if(employees[i].inoffice === true){
statusHTML += "<li class='in'>";//would there be a way to use addClass() here?
}
}
//just trying to put the different ways of doing things
//in some kind of order in my thought process
3 Answers
Steven Parker
231,275 PointsYou can use .addClass on a JQuery object, but not a string.
The code shown is just creating a string. The contents of the string do not represent DOM elements yet.
If the code were redesigned to create the elements immediately, then .addClass or other DOM-manipulation functions could be used on the elements.
nagamani palamuthi
6,160 PointsHi John and Steven thanks for clarifying that. I used addClass on my code and that did not work. so I am just checking if my understanding is right... since in the html we are creating with JS , the <li> items with those classes or any other classes are not there in the original html, this addClass method wont work?
nagamani palamuthi
6,160 PointsI understand now that addClass will not work on a JS object or it has to be converted into a jquery object like $("li").addClass("in"); can someone clarify this pls?
john larson
16,594 Pointsjohn larson
16,594 PointsThanks for clarifying that Steven. I saw that you are going to cut back on the forums...who is going to answer my myriad of questions?