0

I am trying to get the value of an element by class so i could use a switch case to change different element`s values in a table. I seems like the switch case doesn't get the value or i am not using it correctly.

Html code-

<input type="radio" name="Button" class="ButtonState" checked id="Button1" value="1" onclick="setStat1()"/>
<label class="Button" for="Button1">Vagabond</label>
<input type="radio" name="Button" class="ButtonState" id="Button2" value="2" onclick="setStat1()" />
<label class="Button" for="Button2">Warrior</label>

JS-

function setStat1() {
switch (document.getElementsByClassName("ButtonState").value) {
    case 1:
        document.getElementById("vigor").innerHTML = vig;
        document.getElementById("mind").innerHTML = mind;
        document.getElementById("end").innerHTML = end;
        document.getElementById("str").innerHTML = str;
        document.getElementById("dex").innerHTML = dex;
        document.getElementById("int").innerHTML = int;
        document.getElementById("faith").innerHTML = faith;
        document.getElementById("arcane").innerHTML = arcane;
      break;
}
}
Arthur Pinhas
  • 77
  • 2
  • 7
  • `document.getElementsByClassName("Button1").value` produces a string, so it does not match the case for a number. EDIT: also, `getElementsByClassName` would return a collection of items, not just one. – VLAZ Mar 24 '22 at 09:17
  • Arthur - [The first problem](https://stackoverflow.com/questions/10693845/what-do-queryselectorall-and-getelementsby-methods-return) is that `getElementsByClassName` returns a list (`HTMLCollection`), not a single element, so it doesn't have a `value` property. The [second problem](http://stackoverflow.com/questions/32945779/incorrect-result-in-javascript-calculation/32945816#32945816) is that, as @VLAZ says, `value` is a string, and `switch` uses strict `===`, so no string will ever match the number `1`. You need to parse it. – T.J. Crowder Mar 24 '22 at 09:20

0 Answers0