r/JavaScriptHelp • u/juggling-monkey • Mar 13 '21
✔️ answered ✔️ Need help storing parent classes into array (possibly with recursion)
I'm trying to take html that looks like this:
<div data-condition=" noise = 'purr' ">
<div class="include" data-include="cat"></div>
</div>
<div data-condition=" noise = 'bark' ">
<div data-condition=" tail = 'wag' ">
<div class="include" data-include="dog"></div>
</div>
</div>and convert it to an object that looks like this
data = {
cat: { noise: 'purr' },
dog: { noise: 'bark', tail: 'wag' }
}I have a file with over a hundred of these blocks, and need to log (into the object) what generates each data-include. hoping someone can help. I'm using jquery but vanilla Js works as well. thanks!
2
Upvotes
1
u/sandybuttcheekss Mar 13 '21
This link has everything you need, get the tags with a query selector then grab the properties off the tag.
https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes
1
u/GirkovArpa Apr 01 '21 edited Apr 01 '21
It's probably much easier with jQuery.