2

I have a two-dimensional array which I get back as JSON from a call.

and I also have a 3-by-3 div.

as follows:

<div id="type0">
    <div id="image00"></div>
    <div id="image01"></div>
    <div id="image02"></div>
</div><br />

<div id="type1">
    <div id="image10"></div>
    <div id="image11"></div>
    <div id="image12"></div>
</div><br />

<div id="type2">
    <div id="image20"></div>
    <div id="image21"></div>
    <div id="image22"></div>
</div><br />

and i have the loop below to iterate the data.

$.each(res, function (i, tickets) {
     $.each(tickets, function (j, ticket) {

         console.log('i:' + i);
         console.log('j:' + j); 



     });
 });

How can I replace the content of the div above every time this script is invoked?

Essentially, I want to fill up the 3-by-3 div.

So, with the inner loop, I guess I need something like:

$("#type1").$("#image1").html(image here);

What's the correct way to do this?

i and j gives u, 0,1,2 and 0,1,2.

or is there a better way to do this?

2
  • One issue that should be corrected is that the value of ID must be unique within the HTML document. You have id="image0" (and so on) set three times. Commented Jul 15, 2012 at 22:43
  • yes, i just realized that. i will edit the question Commented Jul 15, 2012 at 22:43

2 Answers 2

3

you can try eq() and start with attribute selector:

$.each(res, function (i, tickets) {
     $.each(tickets, function (j, ticket) {
         $('div[id^=img]').eq(j).text(ticket)
     });
 });

$.each(res, function (i, tickets) {
     var ind = i;
     $.each(tickets, function (j, ticket) {
         $('#image'+ind+j).text(ticket)
     });
 });
Sign up to request clarification or add additional context in comments.

3 Comments

how does i adds in here? how can i make image00 ? you are just doing j
@DarthVader :)) just what Darth Vador will say :P [quote]: "I find your lack of faith disturbing." eh ! :)
@DarthVader I have updated ma answer, hope that it can help you.
1

Use the ids as selectors since they are unique

$.each(res, function (i, tickets) {
     $.each(tickets, function (j, ticket) {
         $(('#image'+i)+j).html(image here)
     });
 });

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.