0

hy

I have an issue. I have 4 radio type input, and i want them to be checked which one is selected on my JS function to show a different panel depends on this choice. My problem is when i have to write in my function, in the parameter, how to check if my radio input has been selected or not.

This is my current function (not working):

function function1(){
    if (document.getElementsByClassName("btn1").is(":checked")){
        document.getElementById('div2').style.height = '200px';
    }
    else{
        document.getElementById('div2').style.height = '0px';
    }
    }

<label><input class="btn1" type="radio" name="optradio">Option 1</label>
<label><input class="btn1" type="radio" name="optradio">Option 1</label>
<label><input class="btn1" type="radio" name="optradio">Option 1</label>
<label><input class="btn1" type="radio" name="optradio">Option 1</label>
<button id="btn" onClick="function1()">example 1</button>
Mattia
  • 21
  • 5

1 Answers1

0

The getElementsByClassName() method returns a collection of all elements

like this?

 function function1(){
        if (document.getElementsByClassName("btn1")[0].checked){
            alert("first selected");
        }
        if (document.getElementsByClassName("btn1")[1].checked){
            alert("second selected");
        }
        if (document.getElementsByClassName("btn1")[2].checked){
            alert("third selected");
        }
        if (document.getElementsByClassName("btn1")[3].checked){
            alert("forth selected");
        }
   }
suresh bambhaniya
  • 1,687
  • 1
  • 10
  • 20