JQuery - how to show message if> 1 checkbox is selected?

Depending on the number of ncars that the user has in his entries, flags ncan be selected.

In my opinion, these flags are displayed using a loop

<?php 
foreach ($cars as $row):
echo form_checkbox('cars_id[]', $row->cars_id, FALSE, 'class="checkbox"') . 
<span>'. $row->cars_name . '</span><br />';
endforeach; 
?>

I want to show a message if the user selects> 1 checkbox --- like this:

[x] Honda
[ ] Toyota
[ ] Suzuki

//no message shown


[x] Honda
[X] Toyota
[ ] Suzuki

//message is shown

I tried to do this without success using options

if ($(".checkbox").is(':checked')) { etc...

Does anyone have any suggestions on how to make this work?

Thank you very much for your help.

+3
source share
1 answer

How to display a message if> 1 is selected?

You can check the property of the lengthcheckboxes :checked.

if ($('.checkbox:checked').length > 1) { 
   // Show message.
};

jsFiddle .

In addition, you can select these checkboxes using the :checkboxselector.

+3

All Articles