I have a JSON object like so:
{
"workouts":
[
{
"title": "Full Body",
"exercises":
[
{
"name": "Push Ups",
"duration": 3,
"break": 3
},
{
"name": "Squats",
"duration": 3,
"break": 3
},
{
"name": "Running in Place",
"duration": 3,
"break": 3
}
]
},
{
"title": "God Legs",
"exercises":
[
{
"name": "Running in Place (High Knees)",
"duration": 3,
"break": 3
},
{
"name": "Squats",
"duration": 3,
"break": 3
},
{
"name": "Clams",
"duration": 3,
"break": 3
}
]
},
{
"title": "Morning Stretch",
"exercises":
[
{
"name": "Downward Dog",
"duration": 3,
"break": 3
},
{
"name": "Face Plant",
"duration": 3,
"break": 3
},
{
"name": "Warrior",
"duration": 3,
"break": 3
}
]
}
]
}
I am looping through this object and creating info cards containing the title of each workout ("Full Body", "God Legs" etc).
Upon clicking one of the cards, I would like to be able to store the exercises associated with each title into a variable for further use. For example, If I click on "God Legs", I want the variable to store: "[{'name':'Running in Place (High Knees)', 'duration':3, 'break':3},{'name':'Squats', 'duration':3, 'break':3},{'name': 'Clams', 'duration':3, 'break':3}]"
Here is the code I am using to loop through the JSON data, which is stored on Firebase realtime database, and create the info cards.
JavaScript:
// Initialize Firebase.
firebase.initializeApp(config);
// Reference data.
var dbRef = firebase.database().ref().child("workouts");
// Sync with Firebase in real time.
dbRef.on("value", snap =>
{
var workouts = snap.val();
for (var i = 0; i < workouts.length; i++)
{
//display.innerHTML = exercises[0].name;
var routine = workouts[i].title;
var id = "card" + i;
var $card = ("<li><div class='card' id=" + id + "><a class='startIt' href='timer.html'>\n\
<div class='cardInfo'><h3>" + routine + "</h3><p>10 min.</p>\n\
</div></a><a class='cardOptions' href='overview.html'>\n\
</a></div></li>");
$("#cardList").append($card);
}
});
HTML:
<ul id="cardList" class="cards"></ul>
Thanks for any help and ideas!
where()method - underscorejs.org/#where