How do I listen for changes to a RadioGroup's selected value using JQuery?

Justin picture Justin · Nov 3, 2010 · Viewed 29.9k times · Source

I need to register a handler for a group of radio buttons. I'm using JQuery and hoped that its .change method would accomplish this. However I have not experienced the desired behavoir.

Here is a sample snippet I've written. Sadly, the "radioValueChanged" is only called on the initial load. Selecting either true / false does not trigger the handler.

<html>
<script src="jquery-1.4.2.min.js" type="text/javascript"></script>

<form id="myForm">
    <div id="Question1Wrapper">
        <div>
            <input type="radio" name="controlQuestion" id="valueFalse" value="0" />
            <label for="valueFalse">
                False</label>
        </div>
        <div>
            <input type="radio" name="controlQuestion" id="valueTrue" value="1" />
            <label for="valueTrue">
                True</label>
        </div>
    </div>
    <div id="Question2Wrapper">
        <div>
            <label for="optionalTextBox">
                This is only visible when the above is true</label>
            <input type="text" name="optionalTextBox" id="optionalTextBox" value="" />
        </div>
    </div>

    <script type="text/javascript">
        jQuery(document).ready(function ()
        {
            $("#controlQuestion").change(radioValueChanged('controlQuestion'));
        })

        function radioValueChanged(radioName)
        {
            radioValue = $('input[name=' + radioName + ']:checked', '#myForm').val();

            alert(radioValue);

            if(radioValue == 'undefined' || radioValue == "0")
            {
                $('#Question2Wrapper:visible').hide();
            }
            else
            {
                $('#Question2Wrapper:visible').show();
            }
        } 
    </script>
</form>

Answer

Quintin Robinson picture Quintin Robinson · Nov 3, 2010

There are a few issues here.

  1. You are immediately running radioValueChanged('controlQuestion') upon script execution because that is a method call and not a function assignment.

  2. The selector $("#controlQuestion") is wrong, you don't have any elements with id of controlQuestion.

  3. The radioValueChanged method is not properly handling values as they would be passed to a jQuery event handler.

You could try something like the following:

jQuery(document).ready(function ()
    {
        $("input[name='controlQuestion']").change(radioValueChanged);
    })

    function radioValueChanged()
    {
        radioValue = $(this).val();

        alert(radioValue);

        if($(this).is(":checked") && radioValue == "0")
        {
            $('#Question2Wrapper').hide();
        }
        else
        {
            $('#Question2Wrapper').show();
        }
    } 

In all honesty I'm not sure if that is the actual logic you are looking for with the if statement, but hopefully this will provide a basis for you to correct the current code.