0

jQuery:

var numbers= ["4", "5"];
for (i = 0; i < numbers.length; i++) { 
    var rirs = $(".rirs")
    $(rirs).attr("pl", numbers[i])
} 

This only sets the last element in the array which is 5. such as <div class="rirs" pl="5"> </div> for both divs.

HTML:

<div class="rirs"></div>
<div class="rirs"></div>

2 Answers 2

2

Use eq() method like following.

var numbers= ["4", "5"];
var rirs = $(".rirs");
for (i = 0; i < numbers.length; i++) {         
    rirs.eq(i).attr("pl", numbers[i])
}
Sign up to request clarification or add additional context in comments.

Comments

1

You must specify the index of the div you are selecting, with ":eq()" selector

Try:

var numbers= ["4", "5"];
for (i = 0; i < numbers.length; i++) { 
 var rirs = $(".rirs:eq("+i+")")
 rirs.attr("pl", numbers[i])
} 

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.