• Resolved mjunes

    (@mjunes)


    Hello,

    I’m trying to do execute functions based on checkbox selections. What is the syntax to check a particular selection. Let’s say I have 3 choices blue, green and yellow. I want to execute blue_checked() function if blue choice is checked and if the blue choice is unchecked I want to execute blue_unchecked(). So a total of 6 functions (onchecked and onunchecked) for 3 choices.

    Thank you.

    • This topic was modified 6 years, 1 month ago by mjunes.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author codepeople

    (@codepeople)

    Hello @mjunes,

    There is not an unique way to do that. Assuming the checkbox field is the fieldname1 and the values of choices are: blue, green and yellow, respectively.

    – Using a calculated field:

    Associate the following equation with the calculated field:

    
    (function(){
    var chk = fieldname1|r;
    if(chk.indexOf('blue') != -1) blue_checked();
    if(chk.indexOf('green') != -1) green_checked();
    if(chk.indexOf('yellow') != -1) yellow_checked();
    })()
    

    The modifier |r in the field’s name indicate the plugin that should use the raw value of the field, without modifications or concatenations. In the case of checkbox field returns an array with the values of ticked choices.

    – Using a “HTML Content” field:

    Insert a “HTML Content” field in the form, and enter the following piece of code as its content:

    
    <script>
    jQuery(document).on('change', '[id*="fieldname1_"]', function(){
        jQuery('[id*="fieldname1_"]:checked').each(function(){
            var v = this.value;
            if(v == 'blue') blue_checked();
            if(v == 'green') green_checked();
            if(v == 'yellow') yellow_checked();
        });
    });
    </script>
    

    Best regards.

    Thread Starter mjunes

    (@mjunes)

    The HTML content code did the job. Thank you very much. You guys are the best.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to check a value of a checkbox’ is closed to new replies.