0

I am looping through a json array and append some of its value to a a div. Looping through works fine. But when i have multiple value in the json. It appends only the last value.

Here is my json

{"msg":[{"id":22,"created_at":"2017-01-16 08:57:43","updated_at":"2017-01-16 08:57:43","profile_name":"VFX DESIGNER","experience":"0-2","skill":"s:10:\"MYSQL,JAVA\";","user_id":"2","comments":"sadsads","aptitude":"yes"},
{"id":23,"created_at":"2017-01-16 08:57:43","updated_at":"2017-01-16 08:57:43","profile_name":"MAYA ","experience":"0-2","skill":"s:10:\"MYSQL,JAVA\";","user_id":"2","comments":"sadsads","aptitude":"yes"}]}

When i loop through to get the profile name , it shows only MAYA in the 2nd value. and VFX DESIGNER is not appended.

$.each(data,function(key,value){
        $.each(value,function(index,titleObj){
            $("#profiles_name ul").empty();
            $("#profiles_name ul").append("<a  href = javascript:getCandidates(this) data-value = "+titleObj.user_id+"/"+titleObj.id+"><li>"+titleObj.profile_name+"</li></a><li class = divider></li>");
        });
    });
1
  • Take the .empty() outside of the loop Commented Jan 17, 2017 at 7:59

4 Answers 4

3

Because you are using ("#profiles_name ul").empty(); In every iteraion it removes everything in ("#profiles_name ul") and then appends a new a.

Use the empty() before the first each, it will clean your element and then iterate over the data.

$("#profiles_name ul").empty();

$.each(data,function(key,value){
   $.each(value,function(index,titleObj){
      $("#profiles_name ul").append("<a  href = javascript:getCandidates(this) data-value = "+titleObj.user_id+"/"+titleObj.id+"><li>"+titleObj.profile_name+"</li></a><li class = divider></li>");
   });
});
Sign up to request clarification or add additional context in comments.

Comments

1

Try

$("#profiles_name ul").empty();
$.each(data,function(key,value){
        $.each(value,function(index,titleObj){

            $("#profiles_name ul").append("<a  href = javascript:getCandidates(this) data-value = "+titleObj.user_id+"/"+titleObj.id+"><li>"+titleObj.profile_name+"</li></a><li class = divider></li>");
        });
    });

Empty profiles_name ul before loop

Comments

0

Create a string and then append it to get the desired output.

var str = "";
$.each(data,function(key,value){
    $.each(value,function(index,titleObj){
        str +="<a  href = javascript:getCandidates(this) data-value = "+titleObj.user_id+"/"+titleObj.id+"><li>"+titleObj.profile_name+"</li></a><li class = divider></li>";
    });
});

$("#profiles_name ul").empty();
$("#profiles_name ul").append(str);

Comments

0

Yes right because in each loop you making UL empty and then append newly created html... so: Just once empty UL once:

$("#profiles_name ul").empty();

Then loop through data and append html to it:

$.each(data,function(key,value){
        $.each(value,function(index,titleObj){

            $("#profiles_name ul").append("<a  href = javascript:getCandidates(this) data-value = "+titleObj.user_id+"/"+titleObj.id+"><li>"+titleObj.profile_name+"</li></a><li class = divider></li>");
        });
    });

Hope this will help to you

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.