check if a radio type input is selected on javascript

882 views javascript
6

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';
    }
    }

answered question

1 Answer

1

The getElementsByClassName() method returns a collection of all elements try this

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

posted this

Have an answer?

JD

Please login first before posting an answer.