jqueryradio-buttonicheck

Javascript: get iCheck radio value by name


How to get iCheck radio value by name, like $("input[type=radio][name=test]").val()? Seems it is not working. Please see JSFiddle.

HTML

<div class="i-checks"><label> <input type="radio" checked="" value="a" name="test"> a </label></div>
<div class="i-checks"><label> <input type="radio" value="b" name="test"> b </label></div>
<div class="i-checks"><label> <input type="radio" value="c" name="test"> c </label></div>


The <span id='outputbythis' style='color:green'></span> is checked by $(this).val()
<br>
The <span id='outputbyname' style='color:red'></span> is checked by $("input[type=radio][name=test]").val()

JS

jQuery(document).ready(function ($) {

    $('input').iCheck({
        checkboxClass: 'icheckbox_flat-green',
        radioClass: 'iradio_flat-green'
    });



    $('input').on('ifToggled', function (event) {

        $("#outputbythis").text($(this).val())

        $("#outputbyname").text($("input[type=radio][name=test]").val())

    });

});

Solution

  • That's because the selector will return an array.

    jQuery(document).ready(function ($) {
    
        $('input').iCheck({
            checkboxClass: 'icheckbox_flat-green',
            radioClass: 'iradio_flat-green'
        });
    
    
    
        $('input').on('ifToggled', function (event) {
    
            $("#outputbythis").text($(this).val())
    
            $("#outputbyname").text($("input[type=radio][name=test]:checked").val())
    
        });
    
    });
    

    this should work