Result of checkboxes TO disabled input !how!

I hope I’m not headache, soon my form is over :blush:

I have group of check boxes
input name=“checkbox” type=“checkbox” id=“checkbox_1” value=“ProductName1”
input name=“checkbox” type=“checkbox” id=“checkbox_2” value=“ProductName2”
input name=“checkbox” type=“checkbox” id=“checkbox_3” value=“ProductName3”

and I have a disabled input (locked) on same form, I want when someone click on any of those check boxes, to show in the disabled (locked) input
input name=“PackageRef02” type=“text” disabled id=“PackageRef02” width=“225px”>

Help please :laughing:

1 Like

What happens when more than one checkbox is checked? Do all checked checkboxes appear in the disabled field?

var checkboxes = document.querySelectorAll("input[type=checkbox]");
checkboxes.forEach(function (checkbox) {
    checkbox.addEventListener("click", function (evt) {
        var disabled = document.querySelector("#PackageRef02");
        disabled.value = Array.from(checkboxes)
            .filter(function isChecked(checkbox) {
                return checkbox.checked;
            })
            .map(function (checkbox) {
                return checkbox.value;
            })
            .join(" ");
    });
})

Here’s an example: https://jsfiddle.net/5fm2v5k4/1/

1 Like

Hi Paul,

Yes all what’s checked has to go in box

I guess this is java right! Do I just copy/paste in html? Would you add everything essential for the code to work in html so I copy/paste your code

Thank you

[quote=“kasperkyd, post:3, topic:296497, full:true”]
Would you add everything essential for the code to work in html so I copy/paste your code[/quote]

That cannot be done because your html code is somewhat unique.

I’ve tried to make the javascript code somewhat clear, but to make it even more generic tends to result in making the code harder to understand.

However, an easier to configure version of the code that lets you more easily configure different things about it is:

function isChecked(el) {
    return el.checked;
}
function fieldValue(checkbox) {
    return checkbox.value;
}
function joinCheckboxValues(checkboxes, connector) {
    return checkboxes
        .filter(isChecked)
        .map(fieldValue)
        .join(connector);
}
function showCheckboxValues(opts) {
    function showCheckboxesHandler(evt) {
        var checkboxes = Array.from(opts.checkboxes);
        opts.targetField.value = joinCheckboxValues(checkboxes, " ");
    }
    opts.checkboxes.forEach(function (checkbox) {
        checkbox.addEventListener("click", showCheckboxesHandler);
    });
}
showCheckboxValues({
    checkboxes: document.querySelectorAll("input[type=checkbox]"),
    targetField: document.querySelector("#PackageRef02")
});
1 Like

Thank you sir, will try it right away and let you know :+1:

Is this part of the same process that you asked for in this thread?

You seem to have additional requirements that would affect the js required and of course the display you want (for the disabled input) will require a dummy input (and hidden input) rather than the real thing because you can’t have 2 sizes of text in a normal input.

1 Like

Hi Paul,
I’ve embedded your script here
https://jsfiddle.net/esen3325/

I don’t see the function working, would you check please.

Thank you

Hi Paul,
The size is not a matter, the function is the matter
https://jsfiddle.net/esen3325/
You see the bottom locked input, I want whatever selection value + “number of cases” value to show there.
And then whatever is showing in the locked input will be the value of it “PackageRef02”

Thank you

Move the script to just before the closing body tag otherwise nothing exists when the script is first run.

<script type="text/javascript">
function isChecked(el) {
    return el.checked;
}
function fieldValue(checkbox) {
    return checkbox.value;
}
function joinCheckboxValues(checkboxes, connector) {
    return checkboxes
        .filter(isChecked)
        .map(fieldValue)
        .join(connector);
}
function showCheckboxValues(opts) {
    function showCheckboxesHandler(evt) {
        var checkboxes = Array.from(opts.checkboxes);
        opts.targetField.value = joinCheckboxValues(checkboxes, " ");
    }
    opts.checkboxes.forEach(function (checkbox) {
        checkbox.addEventListener("click", showCheckboxesHandler);
    });
}
showCheckboxValues({
    checkboxes: document.querySelectorAll("input[type=checkbox]"),
    targetField: document.querySelector("#PackageRef02")
});
</script>


</body>
</html>
1 Like

Awesome, now it shows but missing two parts
1- I need when I write something in “Number of cases box” to appear in the locked field too at the beginning (like in the photo).

2- The result shown in locked field still not taken as value of “PackageRef02”
explaining more, when I submit the form, PackageRef02 shows empty, though it shows on the form.

https://jsfiddle.net/f85622zb/

You’re almost there :wink:

Disabled inputs are not posted with the form. If you change the disabled attribute to readonly instead then the value should submit.

Yes that’s what I was referring to when I said “additional requirements” :slight_smile: I will let the other Paul (@Paul_Wilkins) answer that question as it will need to be incorporated into the script.

1 Like

Wow submitting works perfectly wtih readonly Thank youuuuuuuuuuuuuuuu…

I hope other paul fix the missing part and all done.

Ok this code is modified after Paul’s code, which now has eveything I need, the weird thing that the form stop functioning in my theme while Paul’s script functions normally. Note: both function right out of my theme.

Now what is the difference that makes it stop functioning in my theme, something essential changes.

Can you Paul or anyone check what’s the difference between 2 scripts!! Why one if functioning and the other is not!

I feel something wrong in the structure of the script or the way its written

https://jsfiddle.net/4psf70jx/
Thank you

[quote=“kasperkyd, post:13, topic:296497, full:true”]
Ok this code is modified after Paul’s code[/quote]

Modified? I don’t think that any of my code has made it through to your page.

Here’s your scripting code:

        var casesArray = [];
        function onChangeCheckBox(el) {
            var filteredArray = casesArray.filter(e => e === el.value);
            //Add element in casses array if checked
            if (el.checked && filteredArray.length == 0) {
                casesArray.push(el.value)
            }
            else if (!el.checked) {
                //remove it not found
                casesArray = casesArray.filter(e => e !== el.value)
            }
            refreshData();
        }

        function refreshData() {
            document.getElementById("PackageRef02").value = document.getElementById("cases").value+ ' ' + casesArray.join(", ");
        }

And here’s my scripting code:

function isChecked(el) {
    return el.checked;
}
function fieldValue(checkbox) {
    return checkbox.value;
}
function joinCheckboxValues(checkboxes, connector) {
    return checkboxes
        .filter(isChecked)
        .map(fieldValue)
        .join(connector);
}
function showCheckboxValues(opts) {
    function showCheckboxesHandler(evt) {
        var checkboxes = Array.from(opts.checkboxes);
        opts.targetField.value = joinCheckboxValues(checkboxes, " ");
    }
    opts.checkboxes.forEach(function (checkbox) {
        checkbox.addEventListener("click", showCheckboxesHandler);
    });
}
showCheckboxValues({
    checkboxes: document.querySelectorAll("input[type=checkbox]"),
    targetField: document.querySelector("#PackageRef02")
});

There seems to be a simple and easy solution to that problem, use the script that functions normally :slight_smile:

I will however investigate your script to see what can be learned about why it fails.

Thank you Paul :blush: :thumbsup:

Both sets of code seem to place the checked items in the disabled form field.

Can we get more information about what seems to be the problem?

The problem is that with the other script impoted into wordpress theme the script doesn’t work, while if I import your script the function works…
But both scripts do work without the theme…
If it’s same script language why one is rejected while yours is Ok!!

That’s interesting, I haven’t dealt with wordpress themes before. Can you provide a link to a non-working and working wordpress example?

Thank you Paul & Paul, you were so helpful, now I have the final code working for me here

I have 2 issues which will be so easy for you :blush:

1- Why the script doesn’t function in Internet Explorer, but it works ok in Edge and Chrome, can this be fixed !
2- The (readonly) field at the bottom doesn’t pull the input from “Number of Cases” field until any check box is checked,
can we add something to the script that makes the (readonly) input to refresh as soon as “Number of cases” field is updated?

Thank you,

[quote=“kasperkyd, post:19, topic:296497, full:true”]
Thank you Paul & Paul, you were so helpful, now I have the final code working for me here

I have 2 issues which will be so easy for you :blush:

1- Why the script doesn’t function in Internet Explorer, but it works ok in Edge and Chrome, can this be fixed ![/quote]

Ahh yes, the one that we frequently call internet exploder.

With some simple polyfills in place, Internet Explorer can be told how to do those things that it doesn’t know how to do.

if (!Array.from) {
    Array.from = function (els) {
        return Array.prototype.splice.call(els, 0);
    };
}
if (!Object.prototype.forEach) {
    Object.prototype.forEach = function (callback) {
        Array.prototype.forEach.call(this, callback);
    };
}

There are more complex polyfills available, such as in the compatibility section of Array.forEach, but the ones above will work with the code that we have just fine.

[quote=“kasperkyd, post:19, topic:296497, full:true”]
2- The (readonly) field at the bottom doesn’t pull the input from “Number of Cases” field until any check box is checked,
can we add something to the script that makes the (readonly) input to refresh as soon as “Number of cases” field is updated?[/quote]

Yes there is. There are two possible solutions to this. One is to have each part update things separately by itself, and the other is to have a great behemoth of a function that does everything all at once. The former option is the preferable of the two, for it’s easier to maintain things that way.

Currently the JavaScript code adds an event listener on to each checkbox:

    opts.checkboxes.forEach(function (checkbox) {
        checkbox.addEventListener("click", showCheckboxesHandler);
    });

We just need to add an event listener on to the cases input field too.

    opts.cases.addEventListener("change", ...);

Because both the cases and the checkboxes want to update the same form field, it makes sense for each event listener to use the same function that gets the cases number and adds that to the checkbox values.

    function updatePackageReference() {
        var cases = opts.cases.value;
        var checkboxes = Array.from(opts.checkboxes);
        opts.targetField.value = cases + " " + joinCheckboxValues(checkboxes, " ");
    }

Because we now have cases being added too, it makes sense to remove the join from the joinCheckboxValues function, and to join things together in the updatePackageReference() instead.

// function joinCheckboxValues(checkboxes, connector) {
function checkboxValues(checkboxes) {
    return checkboxes
        .filter(isChecked)
        .map(fieldValue);
        // .map(fieldValue)
        // .join(connector);
}
...
    function updatePackageReference() {
        var cases = opts.cases.value;
        var checkboxes = Array.from(opts.checkboxes);
        // opts.targetField.value = cases + " " + joinCheckboxValues(checkboxes, " ");
        opts.targetField.value = [cases].concat(checkboxValues(checkboxes)).join(" ");
    }

We just need to give the opts.cases information when running the code:

showCheckboxValues({
    cases: document.querySelector("#cases"),
    checkboxes: document.querySelectorAll("input[type=checkbox]"),
    ...
});

and tell each event listener to use the same updatePackageReference() function.

    opts.cases.addEventListener("change", updatePackageReference);
    opts.checkboxes.forEach(function (checkbox) {
        checkbox.addEventListener("click", updatePackageReference);
    });

By having each function be responsible for one simple thing, that helps to make the code much easier to update and maintain when changes need to occur.

The updated scripting code is:

if (!Array.from) {
    Array.from = function (els) {
        return Array.prototype.splice.call(els, 0);
    };
}
if (!Object.prototype.forEach) {
    Object.prototype.forEach = function (callback) {
        Array.prototype.forEach.call(this, callback);
    };
}
function isChecked(el) {
    return el.checked;
}
function fieldValue(checkbox) {
    return checkbox.value;
}
function checkboxValues(checkboxes) {
    return checkboxes
        .filter(isChecked)
        .map(fieldValue);
}
function showCheckboxValues(opts) {
    function updatePackageReference() {
        var cases = opts.cases.value;
        var checkboxes = Array.from(opts.checkboxes);
        opts.targetField.value = [cases].concat(checkboxValues(checkboxes)).join(" ");
    }
    opts.cases.addEventListener("change", updatePackageReference);
    opts.checkboxes.forEach(function (checkbox) {
        checkbox.addEventListener("click", updatePackageReference);
    });
}
showCheckboxValues({
    cases: document.querySelector("#cases"),
    checkboxes: document.querySelectorAll("input[type=checkbox]"),
    targetField: document.querySelector("#PackageRef02")
});

The full working code is demonstrated at https://jsfiddle.net/5fm2v5k4/7/

2 Likes