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
182,024 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?