-5

Can someone explain getElementsByClassName, because I thought I was using it correctly, but I am having issues. For instance, the following code doesn't make the hidden elements visible:

var actionButtons = document.getElementsByClassName("action_buttons");
for (var i=0; i < actionButtons.length; i++) {        
    actionButtons[i].style.visibilty = "visible";
}

Yet, if I select by Id I can change the elements' properties. Any idea what the issue is?

I only have "use strict statement" and unused variable warnings so far. And I definitely have four button elements with class="action_buttons".

If I plug in alert(actionButtons[i]); I get object HTMLButtonElement four times...

And here is the relevant HTML:

<button type="button" id="stand_button" class="action_buttons">Stand</button>
<button type="button" id="hit_button" class="action_buttons">Hit</button>
<button type="button" id="double_down_button" class="action_buttons">Double-Down</button>
<button type="button" id="split_button" class="action_buttons">Split</button>
Dave M
  • 418
  • 3
  • 15

2 Answers2

2

Your code has a typo:

style.visibilty

It should be:

style.visibility
Alan Dalton
  • 36
  • 1
  • 4
0

The code you posted is fine, but it only work if an element with class name 'action_buttons' is present in your HTML. Otherwise it will definitely throw error. See the browser console to check for errors, in chrome press f12 to open the console.

May be you are just misspelled the class name either in JavaScript or in HTML.

It should be like, in the case of a button element:

HTML

<button class="action_buttons">Button</button>

JavaScript

var buttons=document.getElementsByClassName("action_buttons")

Also, you dont need to do if(true) checking inside loop.

Nishad K Ahamed
  • 1,374
  • 15
  • 25