jeudi 12 avril 2018

Check checkbox based on variable value

I'm pretty new to jquery/javascript and am struggling with a checkbox. I want the checkbox to be checked depending on a variable coming from the database. I can see the value in my console, so it's dynamically filled, but I can't have the checkbox checked.

I tried 2 things:

 $(document).ready(function() {
$('input[name="OPTIN_NEWSLETTER_STARTER_INDEPENDANT"]').each(function(index) {
        if ($(this).val() == 
"%%OPTIN_NEWSLETTER_STARTER_INDEPENDANT%%")
            ($(this).prop('checked', true));
    });

And

$(document).ready(function() {
    var checkBox = 
[
     ["OPTIN_NEWSLETTER_STARTER_INDEPENDANT", 
"%%OPTIN_NEWSLETTER_STARTER_INDEPENDANT%%"],
];

for (var i = 0; i < checkBox.length; i++) {
        if (checkBox[i][1] == "Yes") {
            if ($('input[name="' + checkBox[i][0] + '"]')) 
            {
                $('input[name="' + checkBox[i][0] + 
'"]').prop("checked", true).change();
            }
        }
    };

This is my html checkbox:

<label class="yesNoCheckboxLabel">
                                        <input type="checkbox" 
name="OPTIN_NEWSLETTER_STARTER_INDEPENDANT" id="control_COLUMN136" 
label="OPTIN_NEWSLETTER_STARTER_INDEPENDANT" 
value="%%OPTIN_NEWSLETTER_STARTER_INDEPENDANT%%" 
checked="">OPTIN_NEWSLETTER_STARTER_INDEPENDANT</label>

It would be great to have someone's insights, thanks already for the help!

Kind regards, Loren




Aucun commentaire:

Enregistrer un commentaire