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

Issey Roquet
Issey Roquet
5,346 Points

How to match JSON with Data Attribute on Multiple Button Onclick

I need a little help with tying JSON, data-attributes and onclick HTML functions.

I have a bottom menu with a list of "badge" buttons with "data-day" attributes (1,2,3...), and a library of cards on top which I fetch from a JSON data file.

Every time a user click on a badge button, I would like JS to fetch the corresponding JSON category so that (JSON) "day": n === "data-day= n" (HTML).

So there are mainly two things to add to this code:

  1. Have the JSON value cardlibrary.cards[i].day match with the HTML data attribute data-day (which I left a 1 value for now),
  2. Add an onclick trigger function on every "badge" button to refresh the card library data when a button is selected.

If there are better ways at going about this, I'm open to suggestions! (I'm new to JS/JSON so my knowledge is quite limited).

Thanks in advance!

Link to JSFiddle: https://jsfiddle.net/j789d523/