I have a custom search page that I want one of the search checkboxes to be pre-selected on page load.
Solution:
- Select the View in the WordPress editor on the page.
- On the sidebar find the CUSTOM JS option and add the Javascript code below:
window.onload = function() {
var labels = document.getElementsByTagName('label');
for (var i = 0; i < labels.length; i++) {
if (labels[i].textContent == 'CHECKBOXLABEL') {
var checkboxId = labels[i].htmlFor;
var checkbox = document.getElementById(checkboxId);
if (checkbox) {
checkbox.click(); // Click the checkbox
}
}
}
};
- Replace CHECKBOXLABEL in the code with the checkbox label you want to be pre-selected.