-1

I have JSONObject with date values. Each date or date range has ID. I used this code to create JSON:

<script>
    <cfset dateStruct = structNew() />
    <cfoutput query="qryOne">
        <cfset dateStruct[userID] = arrayNew(1) />
        <cfloop from="#PickDateTime#" to="#DropDateTime#" index="i" step="#CreateTimeSpan(1,0,0,0)#">
            <cfset arrayAppend(dateStruct[userID],"#dateformat(i,'mmddyyyy')#")/>
        </cfloop>
    </cfoutput>

    var jsonString =  '<cfoutput>#SerializeJSON(dateStruct)#</cfoutput>';
    var JSONObject = JSON.parse(jsonString);

    function giveClass(){
        for(var key in JSONObject){
            res = document.getElementById(JSONObject[key]);
            res.className = 'booked'
        }
    }
</script>

I did alert on jsonString and looks like this:

{"49":["01082016","01092016"],"48":["03012016","03022016","03032016","03042016","03052016","03062016","03072016","03082016","03092016","03102016","03112016","03122016","03132016","03142016","03152016","03162016","03172016","03182016","03192016","03202016","03212016","03222016","03232016","03242016","03252016","03262016","03272016","03282016","03292016","03302016","03312016"],"44":["01122016"],"47":["02062016"],"46":["02112016","02122016","02132016","02142016","02152016"],"35":["01132016"],"36":["01212016"],"39":["01162016"],"37":["01262016"],"38":["01192016"],"43":["01312016"],"42":["02022016","02032016"],"41":["01142016"],"40":["01172016","01182016","01192016"],"51":["01282016"],"52":["02252016","02262016"],"50":["01282016"]}

Each date is stored under unique ID. In my function above I want to assign className = 'booked' not to each ID, I wan to assign to each value. For example if I have id:"42":["02022016","02032016"] I want to give the class name to both values not just id 42. My function gives me class only on id's now, how I can make this to give me a class name for each value?

rrk
  • 15,677
  • 4
  • 29
  • 45
espresso_coffee
  • 5,980
  • 11
  • 83
  • 193

1 Answers1

3

You need to loop through each array:

function giveClass(){
    for(var key in JSONObject){     
        for(var i=0;i<JSONObject[key].length;++i){
            var res2 = document.getElementById(JSONObject[key][i]);   
            res2.className = 'booked';
        }            
    }
}

As Pointy said:

You can drop the JSON directly into your JavaScript code. It doesn't have to be a string and you don't have to parse it explicitly; just create it as plain JavaScript.

espresso_coffee
  • 5,980
  • 11
  • 83
  • 193
Vicky Gonsalves
  • 11,593
  • 2
  • 37
  • 58
  • This works! I totally forgot to loop twice to get all elements for each id. Also small side note, we do not need getEmelentById and className twice, that all can be done after second loop. Thank you! – espresso_coffee Jan 22 '16 at 14:42
  • Glad it worked for you! – Vicky Gonsalves Jan 22 '16 at 14:49
  • I have one more question, do above in my question I have cfloop where I create arrayAppend(dateStruct[userID],"#dateformat(i,'mmddyyyy')#"), I would like to add one more element in array next to i. After I add element I want to loop through and check for that element. How I can get that extra element? – espresso_coffee Jan 22 '16 at 16:57