Shawn
Shawn

Reputation: 3159

jquery target nth instance of class

I have a form with the id of #jobForm. In #jobForm, I can have any number of <div>'s with the class .jIN

In .jIN I always have a single a class called .allCharges

I'm reading data from a json string returned from the web server. There is a json array called charges. in charges, there is a field called block that corresponds to the nth .allCharges. I want to target and populate the correct .allCharges with charges.chargesDesc

I'm doing this:

<form id="jobForm">
  <div class="jIN">
    <div class="allCharges"></div>
  </div>
  <div class="jIN">
    <div class="allCharges"></div>
  </div>
  <div class="jIN">
    <div class="allCharges"></div>
  </div>
</form>

    var cl=json.charges.length;
    for(i=0;i<=cl-1;i++){
      var block=json.charges[i].block;
      $("#jobForm.jIN:eq("+block+") .allCharges").append( json.charges[i].chargeDesc ); 
    }

but that doesn't work.

Upvotes: 0

Views: 113

Answers (1)

ggdx
ggdx

Reputation: 3064

I think

$("#jobForm.jIN:eq("+block+") .allCharges")

should read

$("#jobForm .jIN").eq(block).find(".allCharges").append( json.charges[i].chargeDesc );

You've mashed everything together.

Upvotes: 1

Related Questions