Support » Plugin: cformsII » optgroup in select

  • Resolved Sonika

    (@sonika)


    Hi!

    I need to make optgroup in select in form, and i found the decision, it seems everything is correct but it doesn’t work:

    source: https://www.gilluminate.com/2011/08/08/using-jquery-to-enable-optgroup-in-cforms/

    In header.php:

    <script type="text/javascript">
    if($(".cform").length!=0){
      $(".cform select option").each(function(){
        if($(this).val() == "groupstart"){
          var label = $(this).text();
          $(this).nextUntil('option[value|="groupend"]')
            .wrapAll("<optgroup label='"+label+"' />");
          $(this).detach();
        }
      });
      $('option[value|="groupend"]').detach();
    }
    </script>

    Once you have that code in place, all you need to do is add groupstart and groupend codes to your cforms settings for your select box. For the groupstart you will add it as though it were just another option, with the label being the label you want for your group, and the value being the literal string “groupstart.” For the groupend, it doesn’t matter what the label is, as long as the value is “groupend.” It’s easiest just to use groupend as the label, and leaving the value empty so that CForms will automatically use the label as the value.

    and in form:
    Color#Choose#Warm Colors|groupstart#red#orange#yellow#|groupend#Cool Colors|groupstart#blue#green#purple#|groupend#brown#black#white

    It should be like this:

    https://www.gilluminate.com/wp-content/uploads/optgroup-cforms.png

    But i get this:

    http://oi67.tinypic.com/wjeyk0.jpg
    http://oi64.tinypic.com/5mhleg.jpg

    WHY IT DOESNT WORK?

    • This topic was modified 9 months, 4 weeks ago by  Sonika.
    • This topic was modified 9 months, 4 weeks ago by  Sonika.
Viewing 1 replies (of 1 total)
  • If anyone is interested, I found 1 solution, a bit silly but it works and for me it is enough 🙂

    fuctions.php

    //Replace anything anywhere in the page output
    // https://journalxtra.com/wordpress/snippets/edit-wordpress-page-source-on-the-fly/
    if ( !is_admin() ) {
    
        function vr51_callback($buffer) {
    
            $find = array(
            '<option value="groupstart">ONE</option>', 
            '<option value="groupstart"TWO</option>', 
            '<option value="groupstart">33333</option>',
            '<option value="groupstart">44444</option>',
            '<option value="groupend"></option>'
            );
            $replace  = array(
            '<optgroup label="ONE">', 
            '<optgroup label="TWO">', 
            '<optgroup label="33333">',
            '<optgroup label="44444">',
            '</optgroup>'
            );
    
            $buffer = str_replace($find, $replace, $buffer);
            return $buffer;
            
        }
    
        add_action('wp_loaded', 'buffer_start');    function buffer_start() { ob_start("vr51_callback"); }
        add_action('shutdown', 'buffer_end');       function buffer_end()   { ob_end_flush(); }
        
    }
    //********************* END Replace anything anywhere in the page output 
    • This reply was modified 9 months, 3 weeks ago by  Sonika.
Viewing 1 replies (of 1 total)
  • The topic ‘optgroup in select’ is closed to new replies.