0

Can you please take a look at this code and let me know how I can use the .each() to load each elements of array to the buttons?

var arr=["Left","Middle", "Right" ];
 $("button").each(function(){
        $(this).html(arr);
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="btn-group" role="group" aria-label="...">
  <button type="button" class="btn btn-default">x</button>
  <button type="button" class="btn btn-default">x</button>
  <button type="button" class="btn btn-default">x</button>
</div>

2 Answers 2

1

You certainly can. You use the index of each button in the DOM and use it to iterate through the array

var arr=["Left","Middle", "Right" ];
 $("button").each(function(i){
        $(this).html(arr[i]);
    });

http://jsfiddle.net/d14z32fq/

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

Comments

1

If the array order is the desired, use the index parameter provided by the .each() method:

var arr = ["Left","Middle", "Right"];
$("button").each(function(i){
    $(this).html(arr[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.