I have an array of div names (with no hash), and i’m using the following jQuery code do do something with them:
for(dayList in dayLists) {
dayListID = "#" + dayList;
$(dayListID).append("test");
}
Unfortunately, it does not work. I can see in the console that dayListID is an “HTMLDivElement”, rather than the string that JQ is expecting. How can I join a hash and the div name while keeping the result as a string? Thanks!
Try this:
dayListinfor .. inis not a i-th value of thedayLists, but it is the index. So for array["one", "two"]variabledayListis0, 1and to get the values you must usedayLists[dayList].As Matt wrote in the comments it is good to use
hasOwnPropertyto make sure the properties we are looping through are the objects own.… and then you realize the standard simple
forloop (answer by MДΓΓ БДLL) is actually a better idea. The standardforis also faster.See details HERE.