Using Checkboxes with Select Options

Using Checkboxes with Select Options

I currently have a form that the checkboxes are automatically populated.  There is over 1000 checkboxes in total, 48 of these checkboxes are designed as main topics, when the checkbox for one of these 48 are ticked, it checks all sub catergory checkboxes as well as populates a <select><option> multiple list with its value.  When unchecking one of the 48, it unchecks all its sub categories and removes them from the <select><option> multiple list.

The problem I am having is, when i select one of the 48, which populates the multiple list option and checks all sub categories...  i go to uncheck one in the sub categories, which successfully unchecks the main category (as it indicates they are not all selected).  It also successfully removes the sub category from the multiple select option list and its main category (which became unchecked).

When I go back to check the main category which makes all the sub categories checked...  it re-populates the <select><option> multiple list with the values creating duplicates.  I need a proper way of when I check a box, it scans the multiple <select><option> list to see if it already exists, then adds it again if it is not there.  I have tried the following jquery:

$('#elemSelect option[value = thisCheckbox.value]').length > 0)

but it seems the length always comes out 0 (zero).  I need a fast and efficient way of doing this there is over a 1000 checkboxes that can be checked and need to populate this list.

Any suggestions?










    • Topic Participants

    • mundt