Jump to content

Radio onclick event


erme

Recommended Posts

I have 2 radio buttons.

<input type="radio" name="toasted" id="toasted_sandwiches" value="Toasted Sandwiches" onclick="handleClick();" /> 
<input type="radio" name="toasted" id="toasted_paninis" value="Toasted Paninis" onclick="handleClick();" /> 

I want a function that only adds an event (in this case its spry validation) to what radio button is selected. Currently if the user clicks from one radio to the other, both spry events get called.

function handleClick() {
                if (document.getElementById("toasted_sandwiches").checked == true) {
                    var spry_toasted_sandwiches = new Spry.Widget.ValidationCheckbox("spry_toasted_sandwiches", {minSelections:3, validateOn:["change", "blur"]});
                }
                else if (document.getElementById("toasted_paninis").checked == true) {
                    var spry_paninis = new Spry.Widget.ValidationCheckbox("spry_paninis", {minSelections:3, validateOn:["change", "blur"]});
                }

            }
Link to comment
Share on other sites

Can you use event listeners and jQuery?

 

If so then this could work

<input type="radio" name="toasted" id="toasted_sandwiches" value="Toasted Sandwiches"/>
<input type="radio" name="toasted" id="toasted_paninis" value="Toasted Paninis"/>
<script type="text/javascript">
    $(document).ready(function(){
    
        $( 'input[type=radio][name=toasted]' ).click(function(){
            if( $(this).prop('selected') ){
                switch( $(this).attr('id') ){
                    case 'toasted_sandwiches':
                        var spry_toasted_sandwiches = new Spry.Widget.ValidationCheckbox("spry_toasted_sandwiches", {minSelections:3, validateOn:["change", "blur"]});
                    break;
                    case 'toasted_sandwiches':
                        var spry_paninis = new Spry.Widget.ValidationCheckbox("spry_paninis", {minSelections:3, validateOn:["change", "blur"]});
                    break;
                }
            }
        });
    
    });
</script>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.