I'm trying to iterate an array of JSON object to create a dropdown like this but using nested ul li. It seems this simple code is not working. It generates the ul li as expected but .append is not working. I'm not getting the reason. JSFiddle
<form action="save/" method="post">
<div class="select"></div>
</form>
Javascript
var jsn = [
{
"home": "Home",
"solutions": {
"education": "Education",
"financial_services": "FinancialServices",
"govt": "Government"
},
"products": {
"PCProducts1": "PCPROducts1",
"PCProducts2": "PCPROducts2",
"PCProducts3": "PCPROducts3"
}
}
];
function iterObject(jsn, select){
$.each(jsn, function(key, val){
if(isplain(val)){
ret = createOption(key, val);
$(select).append(ret); //This code is not working
}else{
ret = createGroup(key);
iterObject(val, ret)
}
console.log("ret", ret); // This prints expected output
});
}
function createOption(val, txt){
return '<li rel="'+val+'">'+txt+'</li>';
}
createGroup = function (grpname){
return '<ul "class"="optiongroup" "label"='+grpname+'></ul>';
};
function isplain(data){
if (typeof data === 'number' || typeof data === 'string'){return true;}else{ return false;}
}
iterObject(jsn, '.select');
NOTE UPDATE - Code was not finished while the query was asked. So code in query will not show ul and lis nested. As of now, It should display all theul n lis in .select.
ret? put this code before append.console.log(ret);