• Resolved Dosges

    (@doges)


    Hi,

    I have a form created with Elementor and I need to create a function so that in the form’s checkbox, only 3 options can be selected. I have created a code and tried to add it to function.php but it shows a syntax error.

    The code is the following:

    jQuery(document).ready(function() {
    var checks = jQuery(“.elementor-form input[type=checkbox]”);
    var max = 3;
    for (var i = 0; i < checks.length; i++) checks[i].onclick = selectiveCheck; function selectiveCheck (event) { var checkedChecks = jQuery(“.elementor-form input:checked”); if (checkedChecks.length >= max + 1)
    return false;
    }
    });

    Can someone tell me why these lines of code in the theme’s function.php give me an error? And if there is any different function I can try. I would appreciate an answer from someone.

    Thanks,

    Laura.

Viewing 1 replies (of 1 total)
  • Plugin Support Joel

    (@joelsm)

    Hello,

    Thanks for reaching out!?

    There are a couple of issues in your jQuery code:

    • The quotes around .elementor-form input[type=checkbox] and .elementor-form input:checked are incorrect. Replace the curly quotes (“ ”) with straight quotes (” “) in your jQuery selectors.
    • Make sure your JavaScript code is wrapped in <script> tags if you are adding it directly in your functions.php file.

    Here’s the corrected version of your jQuery code:

    Make sure to add this code inside <script> tags if you are including it directly in your functions.php file.

    jQuery(document).ready(function($) {
        var checks = $(".elementor-form input[type='checkbox']");
        var max = 3;
        
        for (var i = 0; i < checks.length; i++) {
            checks[i].onclick = selectiveCheck;
        }
        
        function selectiveCheck(event) {
            var checkedChecks = $(".elementor-form input:checked");
            if (checkedChecks.length >= max + 1) {
                event.preventDefault();
                // You can add a message here to inform the user that only 3 options are allowed.
                // For example: alert("You can only select up to 3 options.");
            }
        }
    });

    Ensure that there are no JavaScript errors or conflicts with other scripts on your WordPress site, as these can sometimes interfere with your custom jQuery code.

    Please note: that we don’t provide any support for custom codes. This was an exception. The code above is just an example; you probably would want to change something. In such a case, we can’t provide you with any help.

    However, we invite you to join our?Facebook Elementor Community, and check with our users for any recommendations that may have worked for them.

    Wishing you a great day!
    Kind Regards,

Viewing 1 replies (of 1 total)
  • The topic ‘Checkbox limit’ is closed to new replies.