1

I have a simple bit of jquery which is running a trigger on click of a radio button to check my check boxes.

    <script>
        jQuery('#op5').click(function () {
            $('input[type=checkbox]').trigger('click');                

        });
    </script>

These are my checkboxes they have labels to give them a custom style.

<input type="checkbox" certificate="true" checked="checked" id="gridcb0" siteid="1381" value="72218" class="custom selector checkedFocus">
<label for="gridcb0" class="white">&nbsp;</label>

And this is the radio button I am clicking to check the checkboxes

<input type="radio" id="op5" value="1" name="options2" class="custom">

However it is not checking my check boxes on first click it requires me to click on the radio button op5 twice to check the boxes, I do not want to run the line below twice to highlight these check boxes

<input type="radio" id="op5" value="1" name="options2" class="custom">

Suggestions?

Legend1989
  • 664
  • 3
  • 9
  • 23

4 Answers4

2

Use .prop() property and add value checked instead of trigger click If you are using jQuery 1.6+

jQuery('#op5').click(function () {
    $('input[type=checkbox]').prop('checked', true);
});

Docs: https://api.jquery.com/prop/

Sudharsan S
  • 15,336
  • 3
  • 31
  • 49
  • 3
    `attr` expects a string value and changes the literal DOM attribute. You should use [prop](http://api.jquery.com/prop/#prop-propertyName-value) instead which will correctly change the property – CodingIntrigue Apr 21 '15 at 10:22
0

try below code

jQuery('#op5').click(function () {
            $('input[type=checkbox]').attr('checked', true);              
});
nirmal
  • 2,143
  • 1
  • 19
  • 29
0

Which version of jQuery you are using

For jQuery 1.6+, Use the new .prop() function and for jQuery 1.5.x and below, use .attr() function

// jQuery 1.6+
$('#op5').click(function () {
    $('#gridcb0').prop('checked', true);
});

// jQuery 1.5.x and below
$('#op5').click(function () {
    $('#gridcb0').attr('checked', true);
});

Check this post for more details - Setting "checked" for a checkbox with jQuery?

Community
  • 1
  • 1
Adersh M
  • 596
  • 3
  • 19
0

Try prop() and trigger():

Since you have id, you can use id selector

$('#gridcb0').prop('checked', true).trigger('click');

If you still want to target all checkbox:

$('input[type=checkbox]').prop('checked', true).trigger('click');