Skip to content Skip to sidebar Skip to footer

How To Get Child Arrays In Ajax Html()

I have a form for my array of data will print to view with html() and this data each one has child array, I need to get data of those child arrays in my html() as well Screenshots

Solution 1:

Please see my code below

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<body  id="banner">
<ul id="eventCal">
</ul>
<button id="clicks">Click</button>
<script type="text/javascript">
	jQuery(document).ready(function($){

	var quizzes = [{title:'title1',choices:[{choice:'choice1'},{choice:'choice2'}]},
				   {title:'title2',choices:[{choice:'new1'},{choice:'new2'}]},
				   {title:'title3',choices:[{choice:'demo1'},{choice:'demo2'}]}];
	var index   = 0;
	//console.log(quizzes.length)
	$("#clicks").click(function(){
		if(typeof  quizzes[index] != 'undefined'){
			var html = '<li><span>'+quizzes[index].title+'</span></li>';
			if(quizzes[index].choices.length > 0){
			html+='<li class="choises">';
			quizzes[index].choices.forEach((element, index, array) => {
				//console.log(element.title); 
				html+='<ul>';
				html+='<li><span>'+element.choice+'</span></li>';
				html+='</ul>';
			});
			html+='</li>';
			}
			
			
			$("#eventCal").html(html);
			index++;
		}
		if(quizzes.length === index)
			$("#clicks").html("Finish");
		
	})
});

</script>
</body>

Solution 2:


Post a Comment for "How To Get Child Arrays In Ajax Html()"