Legend1989
Legend1989

Reputation: 664

jQuery Select Check Box with Label

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?

Upvotes: 1

Views: 87

Answers (4)

user2575725
user2575725

Reputation:

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');

Upvotes: 0

Adersh M
Adersh M

Reputation: 586

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?

Upvotes: 0

nirmal
nirmal

Reputation: 2180

try below code

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

Upvotes: 0

Sudharsan S
Sudharsan S

Reputation: 15393

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/

Upvotes: 2

Related Questions