javascript - JS for loop showing only last value (Not iterating whole values) -


function addmsg(type, msg) {     if (type = 'new') {         $('#ntfcn').html(type);         var notify = ["new message", "new mail", "new event", "new assignment"];         var i;         (i = 0; < notify.length; i++) {             $('#ntfcn_msg').html("<a href='employee-dashboard.jsp' id='' class='msg_show'><div class='media-body'><h6 class='media-heading'>" + notify[i] + "</h6></div></a>");         }     } } 

this line overwrites what's in ntfcn_msg element on each loop iteration:

$('#ntfcn_msg').html("<a href='employee-dashboard.jsp' id='' class='msg_show'><div class='media-body'><h6 class='media-heading'>" + notify[i] + "</h6></div></a>"); 

you mean have meant append:

$('#ntfcn_msg').append("<a href='employee-dashboard.jsp' id='' class='msg_show'><div class='media-body'><h6 class='media-heading'>" + notify[i] + "</h6></div></a>"); // -------------^ 

...perhaps empty in front of loop clear out first:

$('#ntfcn_msg').empty(); 

so:

function addmsg(type, msg) {     if (type == 'new') {         $('#ntfcn').html(type);         var notify = ["new message", "new mail", "new event", "new assignment"];         var i;         $('#ntfcn_msg').empty();         (i = 0; < notify.length; i++) {             $('#ntfcn_msg').append("<a href='employee-dashboard.jsp' id='' class='msg_show'><div class='media-body'><h6 class='media-heading'>" + notify[i] + "</h6></div></a>");         }     } } 

another option use array#map, join, , html:

function addmsg(type, msg) {     if (type == 'new') {         $('#ntfcn').html(type);         $('#ntfcn_msg').html(             ["new message", "new mail", "new event", "new assignment"].map(function(text) {                 return "<a href='employee-dashboard.jsp' id='' class='msg_show'><div class='media-body'><h6 class='media-heading'>" + text + "</h6></div></a>";             }).join("")         );     } } 

side note: id='' doesn't anything, may leave off.

side note 2: see mike c's point about

if (type = 'new') { 

that should be

if (type == 'new') { 

or

if (type === 'new') { 

= assignment (setting type value 'new'). setting type 'new', testing result, , you'd go body of if because 'new' truthy value.


Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -