javascript - jQuery html code generation with $.get and $.each -
i don't know doing wrong.
var content = ''; $(document).ready(function() { $('a.main_group').click(function(){ var t = $(this), gid = ('' + t.data('id')).split(',')[1]; var div = $('li div.cbp-hrsub div.cbp-hrsub-inner'); $.get(null, {__action: 'get/groups', groupid: gid, languageid: __lngid}, function (d){ var obj = d.action.object; $.each(obj, function(i, el){ $.get(null, { __action: 'get/groups', groupid: el.id, languageid: __lngid}, function (node){ var obj2 = node.action.object; $.each(obj2, function(i, el){ content += "<div><h4>"+el.title+"</h4>"; content += "<ul>"; $.get(null, {__action: 'get/groups', groupid: el.id, languageid: __lngid}, function(node2){ var obj3 = node2.action.object; $.each(obj3, function(i, el){ content += "<li><a href='"+el.url +"'>"+el.title+"</a></li>"; }); }); content += "</ul></div>"; }); }); content += "</div></div>"; console.log(content); }); }); }); });
i need generate html code jquery only: <div></div>
if change every assign content += ''
console.log
, fine. looks content variable being initialized every time. how can fix this?
i think know want try this.
var content = ''; $(document).ready(function() { $('a.main_group').click(function(){ var t = $(this), gid = ('' + t.data('id')).split(',')[1]; var div = $('li div.cbp-hrsub div.cbp-hrsub-inner'); $.get(null, {__action: 'get/groups', groupid: gid, languageid: __lngid}, function (d){ var obj = d.action.object; $.each(obj, function(i, el){ $.get(null, { __action: 'get/groups', groupid: el.id, languageid: __lngid}, function (node){ var obj2 = node.action.object; $.each(obj2, function(i, el){ content += "<div><h4>"+el.title+"</h4>"; content += "<ul>"; $.get(null, {__action: 'get/groups', groupid: el.id, languageid: __lngid}, function(node2){ var obj3 = node2.action.object; $.each(obj3, function(i, el){ content += "<li><a href='"+el.url +"'>"+el.title+"</a></li>"; }); }); content += "</ul></div>"; }); }); content += "</div></div>"; console.log(content); $("your selector here").html(content); }); }); }); });
you need append generated html.
Comments
Post a Comment