0

EDIT:

So, I have following ajax result:

function(data){
    var my_title= data.title; // values: post1,post2,post3      
    var my_name = data.name;  // values: steve,mike,sean

     var my_html ='<div class="'+ my_title +'">'+ my_name +'</div>' 
     jQuery('.my_class').append(my_html);           
};

So, I have two variables (my_titles and my_name) each with 3 values (n number values).

Then, I want to use these individual values for my_html variable and append them in the .my_class.

The result will be as follow:

 <div class="my_class">
    <div class="post1">Steve</div>
    <div class="post2">mike</div>
    <div class="post3">sean</div>
 </div>

Any suggestions to how to achieve this?

Thanks

3
  • 2
    Please show us what is in the my_title variable (what kind of data structure it is). And, describe what exactly you want to do with those values. "run the html function" is not a complete description of what you're trying to accomplish. Commented Jan 18, 2016 at 8:55
  • Is my_title a comma separated string or an array? Commented Jan 18, 2016 at 9:05
  • I made the post more clear with what I am trying to achieve. Please let me know if it is not clear. Thank you so much! Commented Jan 18, 2016 at 9:13

1 Answer 1

2

You have a problem in your code.

Even if you itereate through the array , you're setting the value again and again to all .my_class elements

I assume you're after this :

$.each(data.title,function (i,n){

  jQuery('#my_class').append(n.title);    //or n , we don't know the  structure

})

Or if you have multiple .my_class elements and you want to add data respectively : you can do this

   $.each(data.title,function (i,n){

      jQuery('.my_class').eq(i).html(n.title);    //or n , we don't know the  structure

    })
Sign up to request clarification or add additional context in comments.

3 Comments

That's exactly it! =) Thank you. I will give it a try!
Hi. I updated my post with what I am trying to achieve. Could you take a look at it for me? thanks!
Thank you! =) Learned something new today! =) Appreciate it~

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.