What should I do to make my array not undefined? I used a for loop to place all my buttons in the array. I used the value 1 to my class list to get the 2nd element of my array. The console says undefined. Here is my code.
https://codepen.io/david-lacsao/pen/VwjOZRa?editors=1111
classList[1] accesses the second class in your button’s class attribute. It doesn’t exist. If you use classList[0] they are not undefined.
@Rutherford can you please tell me how I can access the second class in my array if I put the classList[1]. I just thought all my buttons are already placed in the array copyAllButtons . How can I make the second class of my button exist in the array if I put classList[1]?
change classList[1] to classList[0] and look at the output in the console. Is that what you want to achieve?
Your buttons only have one class defined on each one, they don’t have a second class defined (just “btn-red” for example)
@Rutherford I already tried classList[0]. I just want to have an output of the second classes of the button if I put classList[1]
<button class="btn-blue">Cool!</button>
<button class="btn-red">Awsome!</button>
<button class="btn-orange">Great!</button>
<button class="btn-green">Amazing!</button>
Each of the buttons has one class, you can’t access the second class on each one because there isn’t a second class on any of them.
Same question as @Rutherford: is this actually what you want to do (or are you trying to do something else)? What exactly is your aim here?
Okay I am going to show you guys my exact goal but only part of it. I am going to write a bit of code for a little while
@DanCouper here is my code. I already added the function buttonRed(). My goal was to change all the color of the button if I select a certain color in the selection. If I pick red, all the button color would change to red but it doesn’t. I thought the undefined from the classList is what’s causing this. Why doesn’t all button turn red.
Thank you guysss
You are still using 1
for the classList
in the Codepen you linked to. It should be 0
(all_buttons[i].classList[0]
)
@lasjorg thanks for the help. I can finally rest now