I need to write a jQuery which would append the JSON Data directly into the a 'div' next to the appropriate 'div' in the HTML. While the actual data is more vast and complicated, I'm putting a simple version for this question.
The html is as below:
<div class="Category">
<div class="Sub-Category" id="Apple">
<div id="Parameters"></div>
<div id="Technicalities"></div>
</div>
<div class="Sub-Category" id="Samsung">
<div id="Parameters"></div>
<div id="Technicalities"></div>
</div>
</div>
The JSON data which needs to be added into this is as such:
var StatJSON = {
"Apple":{
"Parameters": "Parameter1-2000/Parameter2-5000/Parameter3-3000",
"Technicalities": "Technicality1-2000/Technicality2-5000/Technicality3-3000",
},
"Samsung":{
"Parameters": "Parameter1-2000/Parameter2-5000/Parameter3-3000",
"Technicalities": "Technicality1-2000/Technicality2-5000/Technicality3-3000",
}
}
The jQuery I have attempted for this, but seems messed up and 'am unable to crack:
$(document).ready(function (){
var StatJsonKeys = Object.keys(StatJSON);
jQuery('.Category .Sub-Category').each(function ($) {
for (i=0; i<StatJsonKeys.length; i++){
if (jQuery(this).text() == StatJsonKeys[i]) {
if (jQuery(this).children().each(function() {
for (j=0; j<StatJsonKeys.length; i++){
jQuery(this).attr('id') == StatJsonKeys[i].keys() {
jQuery(this).append('<div>' + StatJsonKeys[i].values() + '</div>')
}
}
}
}
}
});
I'm still a novice with this, especially handling JSON, would appreciate any help with this. Thanks!!
$(document).ready(function (){
var StatJSON = {
"Apple":{
"Parameters": "Parameter1-2000/Parameter2-5000/Parameter3-3000",
"Technicalities": "Technicality1-2000/Technicality2-5000/Technicality3-3000",
},
"Samsung":{
"Parameters": "Parameter1-2000/Parameter2-5000/Parameter3-3000",
"Technicalities": "Technicality1-2000/Technicality2-5000/Technicality3-3000",
}
}
var StatJsonKeys = Object.keys(StatJSON);
jQuery('.Category .Sub-Category').each(function ($) {
for (i=0; i<StatJsonKeys.length; i++){
if (jQuery(this).text() == StatJsonKeys[i]) {
if (jQuery(this).children().each(function() {
for (j=0; j<StatJsonKeys.length; i++){
jQuery(this).attr('id') == StatJsonKeys[i].keys() {
jQuery(this).append('<div>' + StatJsonKeys[i].values() + '</div>')
}
}
}
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<body>
<div class="Category">
<div class="Sub-Category" id="Apple">
<div id="Parameters"></div>
<div id="Technicalities"></div>
</div>
<div class="Sub-Category" id="Samsung">
<div id="Parameters"></div>
<div id="Technicalities"></div>
</div>
</div>
</body>