How to get value of selected radio button?

前端 未结 28 2702
温柔的废话
温柔的废话 2020-11-22 03:51

I want to get the selected value from a group of radio buttons.

Here\'s my HTML:

28条回答
  •  南方客
    南方客 (楼主)
    2020-11-22 04:50

    Use document.querySelector('input[type = radio]:checked').value; to get value of selected checkbox , you can use other attributes to get value like name = gender etc. please go through below snippet definitely it will helpful to you,

    Solution

    document.mainForm.onclick = function(){
        var gender = document.querySelector('input[name = gender]:checked').value;
        result.innerHTML = 'You Gender: '+gender;
    }
    Male Female Others

    Thank-You

提交回复
热议问题