Home > Enterprise >  Automatically have all checkboxes selected on page load
Automatically have all checkboxes selected on page load

Time:02-17

I can find a lot of code for a "select all" option, but what I want is more straightforward. I want the WpForms checkboxes all checked by default on page load, not when a button is pressed. I want them all selected so users only have to de-select the few they don't want.

I am trying to use this code currently, please help.

$(':checkbox').each(function() {
    this.checked = true;                        
});

Thanks for any help. I posted the above code into a javascript wordpress plugin but it didn't help.

CodePudding user response:

on HTML just add checked as attribute for input type="checkbox"

<input type="checkbox" name="test1" value="test1" checked> I'm Check<br>
<input type="checkbox" name="test2" value="test2" checked> I'm Check<br>
<input type="checkbox" name="test3" value="test3"> I'm not Check<br>
<input type="checkbox" name="test4" value="test4" checked> I'm Check

CodePudding user response:

First of all my friend, you are not a retard ! You just need to gain more experience ;)

You should get checkboxes like this in Jquery and check them all:

$('input[type=checkbox]').each(function () {
    this.checked = true;
});

This might help you.

About that wordpress plugin that you mentioned I have to say that you should put this piece of code in a script tag and a html file which has the checkBoxes in it.

CodePudding user response:

you can use jquery for this solution

$(document).ready || use for apply any function on load page

Solution:

<input type="checkbox" name="test1" value="test1"> Test 1<br>
<input type="checkbox" name="test2" value="test2"> Test 2<br>
<input type="checkbox" name="test3" value="test3"> Test 3<br>
<input type="checkbox" name="test4" value="test4"> Test 4


$(document).ready(function(){
$('[type="checkbox"]').each(function(){
$(this).attr("checked","checked");
});
})
  • Related