I'm new to jQuery/JSON and I've build an dynamic toggle system. It work fine, but I'm not sure about my code and I want a better way of building this.
Could you please see my code and give comments/solutions/explanations for a better structure for this system?
NOTE: All JSON files are the same build
$(document).ready(function() {
$(".color-list.one li:nth-child(2)").on('click', function() {
$.getJSON("json/a2.json", function(data) {
//Handle my response
$('ul.elements-list').html(
'<li class="elements-item"><span class="tog">' + data.Atitle + '</span>'+
'<div class="togcont hidden">'+
'<ul class="elements-link">'+
'<li><a href=' + data.AAurlElement + '>' + data.AAnameElement + '</a></li>'+
'<li><a href=' + data.ABurlElement + '>' + data.ABnameElement + '</a></li>'+
'</ul>'+
'</div></li>');
$('ul.elements-list').append(
'<li class="elements-item"><span class="tog">' + data.Btitle + '</span>'+
'<div class="togcont hidden">'+
'<ul class="elements-link">'+
'<li><a href=' + data.BAurlElement + '>' + data.BAnameElement + '</a></li>'+
'</ul>'+
'</div></li>');
});
});
$(".color-list.one li:nth-child(3)").on('click', function() {
$.getJSON("json/a3.json", function(data) {
//Handle my response
$('ul.elements-list').html(
'<li class="elements-item"><span class="tog">' + data.Btitle + '</span>'+
'<div class="togcont hidden">'+
'<ul class="elements-link">'+
'<li><a href=' + data.BAurlElement + '>' + data.BAnameElement + '</a></li>'+
'<li><a href=' + data.BBurlElement + '>' + data.BBnameElement + '</a></li>'+
'</ul>'+
'</div></li>');
});
});
$(".color-list.one li:nth-child(4)").on('click', function() {
$.getJSON("json/a4.json", function(data) {
//Handle my response
$('ul.elements-list').html(
'<li class="elements-item"><span class="tog">' + data.Atitle + '</span>'+
'<div class="togcont hidden">'+
'<ul class="elements-link">'+
'<li><a href=' + data.AAurlElement + '>' + data.AAnameElement + '</a></li>'+
'<li><a href=' + data.ABurlElement + '>' + data.ABnameElement + '</a></li>'+
'<li><a href=' + data.ACurlElement + '>' + data.ACnameElement + '</a></li>'+
'</ul>'+
'</div></li>');
$('ul.elements-list').append(
'<li class="elements-item"><span class="tog">' + data.Btitle + '</span>'+
'<div class="togcont hidden">'+
'<ul class="elements-link">'+
'<li><a href=' + data.BAurlElement + '>' + data.BAnameElement + '</a></li>'+
'</ul>'+
'</div></li>');
});
});
$(".color-list.one li:nth-child(5)").on('click', function() {
$.getJSON("json/a5.json", function(data) {
//Handle my response
$('ul.elements-list').html(
'<li class="elements-item"><span class="tog">' + data.Atitle + '</span>'+
'<div class="togcont hidden">'+
'<ul class="elements-link">'+
'<li><a href=' + data.AAurlElement + '>' + data.AAnameElement + '</a></li>'+
'<li><a href=' + data.ABurlElement + '>' + data.ABnameElement + '</a></li>'+
'<li><a href=' + data.ACurlElement + '>' + data.ACnameElement + '</a></li>'+
'</ul>'+
'</div></li>');
$('ul.elements-list').append(
'<li class="elements-item"><span class="tog">' + data.Btitle + '</span>'+
'<div class="togcont hidden">'+
'<ul class="elements-link">'+
'<li><a href=' + data.BAurlElement + '>' + data.BAnameElement + '</a></li>'+
'</ul>'+
'</div></li>');
});
});
$('ul.elements-list').on("click", ".tog", function(){
var obj = $(this).next();
if(obj.hasClass("hidden")){
obj.removeClass("hidden").slideDown();
$(this).addClass("bounce");
} else {
obj.addClass("hidden").slideUp();
$(this).removeClass("bounce");
}
});
});
The structure of my json files :
{
"Atitle": "Master Title A",
"AAnameElement": "name 1",
"ABnameElement": "name 2",
"ACnameElement": "name 3",
"AAurlElement" : "url1.html",
"ABurlElement" : "url2.html",
"ACurlElement" : "url3.html",
"Btitle": "Master Title B",
"BAurlElement" : "urlB1.html",
"BAnameElement": "name B1"
}