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 AJAX Basics (retiring) Programming AJAX Processing JSON Data

I'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
Steven Parker
231,269 Points

:point_right: You 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.

Thanks 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?

Hi 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?

I 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?