0

I am trying to loop over my array called topics. This array has names of people. Within my loop, i want to grab each name and plug it into a button with its text.

When i console.log my loop, i can see each name in my array get printed. But when i try to append the text of my buttons, it only adds the last name in my array.


var topics = ["Lebron James", "Kevin Durant", "Steph Curry", "James Harden", "Russel Westbrook", "Giannis Antetokounmpo", "Kawhi leonard", "Anthony Davis", "Paul George", "Kyrie Irving"];

for (var i = 0; i < topics.length; i++) {
    $(".gifnames").append("<button type=\"button\" class=\"btn btn-primary\">");
    $("button").text(topics[i]);
    console.log(topics[i]);
}

only Kyrie irvings name is added to all buttons instead of each name being added to the buttons created.

5 Answers 5

2

$("button") matches all the buttons in the document. Thus, in each iteration it reset's all the previously button's text with the current item. Hence, in the final iteration of the loop it replaces all the button's text with the last item of the array.

You have to target the specific button. You can use jQuery's .eq() like the following way:

var topics = ["Lebron James", "Kevin Durant", "Steph Curry", "James Harden", "Russel Westbrook", "Giannis Antetokounmpo", "Kawhi leonard", "Anthony Davis", "Paul George", "Kyrie Irving"];

for (var i = 0; i < topics.length; i++) {
  $(".gifnames").append("<button type=\"button\" class=\"btn btn-primary\">");
  $("button:eq(" + i + ")").text(topics[i]);
  console.log(topics[i]);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="gifnames"></div>

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

Comments

1

With the next line inside your loop you are changing the text of all the buttons:

$("button").text(topics[i]);

Because the used selector will match all buttons tags available on the document. So, the last iteration of the loop sets the last text of the array to all the buttons. One solution is to first create the button, add the related text to it, and finally append it to the container:

var topics = ["Lebron James", "Kevin Durant", "Steph Curry", "James Harden", "Russel Westbrook", "Giannis Antetokounmpo", "Kawhi leonard", "Anthony Davis", "Paul George", "Kyrie Irving"];

for (var i = 0; i < topics.length; i++)
{
    $('<button type="button" class="btn btn-primary">')
        .text(topics[i])
        .appendTo(".gifnames");
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="gifnames"></div>

Comments

0

what if you change to this ?

for (var i = 0; i < topics.length; i++) {
    var nodes = "<button type='button' class='btn btn-primary'>"+topics[i]+"</button>";
    $(".gifnames").append(nodes);
    //$("button").text(topics[i]);
    console.log(topics[i]);
  }

Comments

0

You can add an attribute to each button, and then select each button by that attribute

var topics = ["Lebron James", "Kevin Durant", "Steph Curry", "James Harden", "Russel Westbrook", "Giannis Antetokounmpo", "Kawhi leonard", "Anthony Davis", "Paul George", "Kyrie Irving"];

for (var i = 0; i < topics.length; i++) {
 $(".gifnames").append("<button type=\"button\" data-id=\""+topics[i]+"\" class=\"btn btn-primary\">");
  $("button[data-id=\""+topics[i]+"\"]").text(topics[i]);
  console.log(topics[i]);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="gifnames"></div>

Comments

0

Here is a vanilla JS way to do it.

var topics = ["Lebron James", "Kevin Durant", "Steph Curry", "James Harden", "Russel Westbrook", "Giannis Antetokounmpo", "Kawhi Leonard", "Anthony Davis", "Paul George", "Kyrie Irving"];
var gifnames = document.getElementsByClassName("gifnames")[0];

for (var i = 0; i < topics.length; i++) {
  var b = document.createElement("button");
  b.classList.add("btn", "btn-primary");
  b.name = topics[i];
  b.innerHTML = topics[i];
  gifnames.appendChild(b);
}
.btn-primary {
  background-color: aliceblue
}
.btn {
  display: block
}
<div class="gifnames">
</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.