PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Automatically change value of multiple radio buttons

  1. I've tried lots of different ways of doing this, but am having no luck at-all;( I would like to be able to have multiple sets of radio buttons as follows:

    radio buttons field 1:
    <red><blue><green>

    radio buttons field 2: (further down form)
    <red><blue><green>

    radio buttons field 3: (even further down form!)
    <red><blue><green>

    When someone clicks <red> in any of the 3 fields above, I'd like the ALL of the radio buttons in each field set to change to 'red' wherever they appear on the form.

    I'm guessing this could be done with JQuery (already loaded on page) - but unfortunately I just can't work out how to do it (I'm a n00b at javascript...sorry!)

    I did try and setup multiple conditional displays of the radio button fields with the correct option selected - but the above is a very simplified example - and it was getting crazy with the amount of form fields I had (and I got some strange things happening) - this is not going to work for me.

    I certainly think JQuery could be the way forward on this targeting the field ID's but I just have no idea of where to start - any help would be appreciated thank you!

    Posted 12 years ago on Saturday May 26, 2012 | Permalink
  2. If anyone else comes across this issue, here was my Javascript solution - works perfect for my radio buttons. Hope it helps someone else out - also works with multi-part forms!

    Basically this synchronizes fieldset values - so if you have multiple field sets down a page that all need to change if *one* is changed - this will do it. Although this was developed as a separate plugin, it should also work just fine in your theme functions file.

    add_action('get_header', 'myplugin_gforms_multi_val_do');
    
    /*
    * JS to control radio buttons where required
    * //TODO: Change to required page(s) to include in right places
    * @since 1.0
    */
    function myplugin_gforms_multi_val_do() {
    	if (is_page('my-form-page') || is_page('my-form-page-other')) {
    		add_action('wp_enqueue_scripts', 'myplugin_gforms_multi_val_jq');
    		add_action('wp_footer', 'myplugin_gforms_multi_val_js');
    	}
    }
    
    /*
    * Insert JQuery to control radio buttons where required
    * @since 1.0
    */
    function myplugin_gforms_multi_val_jq() { wp_enqueue_script( 'jquery' ); }
    
    /*
     * Insert JS to control radio buttons where required
     * //TODO: Change $target_vals array to required fields
     * @since 1.0
     */
    function myplugin_gforms_multi_val_js() {
    	if (is_page('testing')) {
    
    		$target_vals = array('blue','red','gold','purple');
    
    		echo "\n".'<script type="text/javascript">';
    		echo "\n".'jQuery.noConflict();';
    		echo 'jQuery(document).ready(function($) {';
    
    		foreach ($target_vals as $val) {
    			echo "$('[value=\"".$val."\"]').change(function(){";
    			echo "$('[value=\"".$val."\"]').prop('checked',true);";
    			echo "});";
    		}
    
    		echo "});"."\n";
    		echo "</script>"."\n";
    
    	}
    }
    Posted 12 years ago on Wednesday May 30, 2012 | Permalink
  3. Dang it! Anyone got any ideas on how to fix this please - my js above controls the multiple radio button sets perfectly - UNTIL you are also doing conditional display of fields controlled by said radio buttons... you have to click on the radio button twice to trigger the conditional display of the field;(

    Posted 12 years ago on Thursday May 31, 2012 | Permalink