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
https://forums.phpfreaks.com/topic/287512-radio-onclick-event/
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
https://forums.phpfreaks.com/topic/287512-radio-onclick-event/#findComment-1475120
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.