Getting the selected radio without using “Id” but “name”

后端 未结 6 659
逝去的感伤
逝去的感伤 2020-12-19 03:17

I was trying to get selected radio button by using \"document.getElementByName(\'nameOfradio\')\" because all of the radio buttons share the same name. But, nothing happened

6条回答
  •  执念已碎
    2020-12-19 03:27

    document.querySelector('input[name=nameOfRadio]:checked').value
    

    Eg:-

    Male
    Female
    Other
    document.querySelector('input[name=gender]:checked').value

    Also, you can add a checked attribute to a default radio button among the group of radio buttons if needed

     Male
    Female
    Other

提交回复
热议问题