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

后端 未结 6 665
逝去的感伤
逝去的感伤 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:32

    This line:

    document.getElementByName('nameOfradio').value
    

    should be:

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

    using querySelector

    Note that CSS pseudo-classes are accessed by a colon (:).

提交回复
热议问题