Home > other >  Jquery how to get list of checked radio button value?
Jquery how to get list of checked radio button value?

Time:11-06

I have this jquery script

  function radiolist() {
        var radioChecked = [];
        var radios = $("input[type='radio']");
        //alert(radios.length);
        
        for (var i = 0; i < radios.length; i  ) {
            if (radios[i].checked) {
                radioChecked.push(radios[i].val());
            }
        }
        alert(radioChecked);
    }

I want to save the list of checked radio buttons in the radioChecked array and access them. But I get the following error

Uncaught TypeError: radios[i].val is not a function

Why can`t to get radio button value ?

CodePudding user response:

When you index an element in jQuery, it doesn't rewrap the element in jQuery. So, you can either use the vanilla javascript approach with radios[i].value, or rewrap it in jQuery with $(radios[i]).val().

  • Related