javascript - How to change the value of a JSON data while diplaying in a list? -
i getting data database key value pairs , trying populate values in list view. while looping change value of data (item.student_class) 'your next class 2' if value class1. when try below, changes values in list.
var buffer=""; $.each(data, function(index, val){ for(var i=0; < val.length; i++){ var item = val[i]; //if student's class class1, change value below if(item.student_class= 'class1'){var studentclass='your next class 2';} buffer+='<li id="' + item.student_id+ '" data-student_class="'+studentclass+'"><a href="#"><b>' + item.student_name+'</b><span class="af-badge" style="background-color:#4a4">'+studentclass+'</span><br/>'+item.join_date+'</a></li>'; } $('#student_list').html(buffer); });
could please me how can acheive ?
fix conditional not assignment. initializing variable inside condition , might undefined in cases. set "" first: i.e.
var buffer = ""; $.each(data, function (index, val) { (var = 0; < val.length; i++) { var studentclass = ""; var item = val[i]; //if student's class class1, change value below if (item.student_class == 'class1') { studentclass = 'your next class 2'; } buffer += '<li id="' + item.student_id + '" data-student_class="' + studentclass + '"><a href="#"><b>' + item.student_name + '</b><span class="af-badge" style="background-color:#4a4">' + studentclass + '</span><br/>' + item.join_date + '</a></li>'; } $('#student_list').html(buffer); });
Comments
Post a Comment