21

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>

Charles Sprayberry
  • 7,741
  • 3
  • 41
  • 50
Justin
  • 10,667
  • 15
  • 58
  • 79

2 Answers2

34

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.

Quintin Robinson
  • 81,193
  • 14
  • 123
  • 132
  • Awesome thanks. As you can tell javascript / jquery is fairly new / rusty. So '#' always prefixes a id not a name... good to know:) What exactly does "$(this).is(":checked") do? I'm thinking I want the opposite of that. As in if no value is selected I want the question hidden. – Justin Nov 03 '10 at 21:25
  • 1
    @Justin Yes the selectors actually follow CSS selector rules, you can search w3 for css selectors.. hierarchy, name, id etc. Also the `is` is a jQuery function: http://docs.jquery.com/Is (although I didn't find it on api.jquery.com which concerns me). – Quintin Robinson Nov 03 '10 at 21:36
  • `is()` is *definitely* at the api subdomain: [api.jquery.com/is/](http://api.jquery.com/is/). – David Thomas Nov 03 '10 at 22:18
  • @David Thomas thanks, I didn't actually attempt to append "is" to the api url and navigate although I did contemplate it. BTW you made me hungry with ricebowl! – Quintin Robinson Nov 03 '10 at 22:27
0
$('#Question2Wrapper:visible').show();

Take out the :visible, this will only select it if the div is already showing, in effect it will never be shown if it is hidden.

$('#Question2Wrapper').show();

I digress, I think Quintin hits most of the points. There are a few issues going on here.

John Strickler
  • 25,151
  • 4
  • 52
  • 68