Click here to Skip to main content
13,197,200 members (56,591 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I have a multiselect dropdown which has 8 colors in a hash %all_colors:

my %all_colors = (
1 => 'Red',
2 => 'Yellow',
3 => 'Orange',
4 => 'Blue',
5 => 'Black',
6 => 'Brown',
7 => 'Green',
8 => 'White',

I have put it in a dropdown like this:

my $color_selector = '<select name="all_colors">';

foreach my $color (sort {$all_colors{$a} cmp $all_colors {$b}} keys %all_colors ) {
$color_selector .= qq~<option value="$color">$all_colors{$color}</option>~;
$color_selector .= '</select>';

<% $color_selector %>

Now I want to add 2 new checkboxes.

<input type="checkbox" name="maincolors" value="1" class="inputCheckbox" /> Main Colors
<input type="checkbox" name="resetofcolors" value="1" class="inputCheckbox" /> Rest of the Colors

And in Perl, I added one more constant to select checkbox1 and use constant in perl to call the checkbox. I dont know how to use this to select these 4 values in dropdown when "Main Colors" checkbox is checked. And Rest of colors should be checked when clicked on "Rest of the Colors" checkbox.

use constant MAIN_COLORS => {
1 => 'Red',
2 => 'Orange',
3 => 'Green',
4 => 'White',

my $main_colors = MAIN_COLORS;

I have written a code which uses plain HTML but I actually want to call the colors from Perl hash

<select id="multipeColorSelect" multiple size="8">
<option value="Red" class="maincolor">Red</option>
<option value="Orange" class="maincolor">Orange</option>
<option value="Green" class="maincolor">Green</option>
<option value="White" class="maincolor">White</option>
<option value="Yellow">Yellow</option>
<option value="Blue">Blue</option>
<option value="Black">Black</option>
<option value="Brown">Brown</option>

But I dont want this to use plain HTML, instead I want :
1) If I select checkbox1 "Main Colors", it should automatically select Red, Orange, Green, White from the dropdown ie., it should call values from hash
2) If I select checkbox2 "Rest of the Colors", it should automatically select the rest of the colors from the dropdown ie., it should call values from hash

Please help.

Also, There are some conditions which is not working in jQuery as expected. I need help for jQuery also.

1) If any 1 color is randomly selected from the dropdown instead of checking checkbox, then I want to disable the checkbox which is not related to that group.
Ex: If Red is selected from dropdown directly, "Rest of the Colors" checkbox should be disabled. If I deselect Red and select Brown directly, then "Rest of the colors"
checkbox should be reenabled and "Main Colors" checkbox should be disabled.
2) One set of four elements need not be disabled when others are selected. Both can be selected simutlaneously so that all are selected by checking both checkboxes.
Users are allowed to select and deselect members of the group of four which is already working in jQuery code below.
3) If "White" and "Yellow" both are selected at the same time from dropdown, then both checkboxes should be enabled.

$('input[name="colorcheckbox"]').click(function () {

var colorsToSelect = $(this).val();

if($(this).prop('checked') == true) {
if(colorsToSelect == 'maincolors') {
$('#multipeColorSelect option').slice(0,4).prop('selected', true);
} else if (colorsToSelect == 'restofcolors') {
$('#multipeColorSelect option').slice(4,8).prop('selected', true);
} else {
if(colorsToSelect == 'maincolors') {
$('#multipeColorSelect option').slice(0,4).prop('selected', false);
} else if (colorsToSelect == 'restofcolors') {
$('#multipeColorSelect option').slice(4,8).prop('selected', false);

Please help.
Posted 22-Oct-14 9:27am

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.171020.1 | Last Updated 22 Oct 2014
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100