473,513 Members | 3,621 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Perl and jQuery to select elements from multiselect dropdown

1 New Member
I have a multiselect dropdown which has 8 colors in a hash %all_colors:

Expand|Select|Wrap|Line Numbers
  1.     my %all_colors = (
  2.         1 => 'Red',
  3.         2 => 'Yellow',
  4.         3 => 'Orange',
  5.         4 => 'Blue',
  6.         5 => 'Black',
  7.         6 => 'Brown',
  8.         7 => 'Green',
  9.         8 => 'White',
  10.     );
I have put it in a dropdown like this:

Expand|Select|Wrap|Line Numbers
  1.     my $color_selector = '<select name="all_colors">';
  2.  
  3.     foreach my $color (sort {$all_colors{$a} cmp $all_colors {$b}} keys %all_colors ) {
  4.         $color_selector .= qq~<option value="$color">$all_colors{$color}</option>~;
  5.     }
  6.     $color_selector .= '</select>';
  7.  
  8.     <div><% $color_selector %></div>
Now I want to add 2 new checkboxes.

Expand|Select|Wrap|Line Numbers
  1.     <div>
  2.         <input type="checkbox" name="maincolors" value="1" class="inputCheckbox" /> Main Colors
  3.         <input type="checkbox" name="resetofcolors" value="1" class="inputCheckbox" /> Rest of the Colors
  4.     </div>
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.

Expand|Select|Wrap|Line Numbers
  1.     use constant MAIN_COLORS => {
  2.         1 => 'Red',
  3.         2 => 'Orange',
  4.         3 => 'Green',
  5.         4 => 'White',
  6.     };
  7.  
  8.     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

Expand|Select|Wrap|Line Numbers
  1. <select id="multipeColorSelect" multiple size="8">
  2.     <option value="Red" class="maincolor">Red</option>
  3.     <option value="Orange" class="maincolor">Orange</option>
  4.     <option value="Green" class="maincolor">Green</option>
  5.     <option value="White" class="maincolor">White</option>
  6.     <option value="Yellow">Yellow</option>
  7.     <option value="Blue">Blue</option>
  8.     <option value="Black">Black</option>
  9.     <option value="Brown">Brown</option>
  10. </select>
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.

Expand|Select|Wrap|Line Numbers
  1. $('input[name="colorcheckbox"]').click(function () {
  2.  
  3.     var colorsToSelect = $(this).val();
  4.  
  5.     if($(this).prop('checked') == true) {
  6.         if(colorsToSelect == 'maincolors') {
  7.             $('#multipeColorSelect option').slice(0,4).prop('selected', true);
  8.         } else if (colorsToSelect == 'restofcolors') {
  9.             $('#multipeColorSelect option').slice(4,8).prop('selected', true);
  10.         } 
  11.     } else {
  12.         if(colorsToSelect == 'maincolors') {
  13.             $('#multipeColorSelect option').slice(0,4).prop('selected', false);
  14.         } else if (colorsToSelect == 'restofcolors') {
  15.             $('#multipeColorSelect option').slice(4,8).prop('selected', false);
  16.         }
  17.     }
  18. });
Please help.
Oct 22 '14 #1
1 2406
numberwhun
3,509 Recognized Expert Moderator Specialist
For the options that are labeled "maincolor", you want to add the "selected" option to the definition, like so:

Expand|Select|Wrap|Line Numbers
  1. <option value="Red" class="maincolor" selected>Red</option>
  2.  
As for the jquery portion, since this is the Perl forum, I will ask you to please post that portion in the jquery forum, where you will find others more able to answer your question.

Regards,,

Jeff
Nov 2 '14 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
9998
by: Rod Early | last post by:
I need to know when the select element's dropdown list is opened (as when the user clicks on the arrow or does ALT-downarrow from the keyboard). Similarly, I need to known when the dropdown list closes. The closing of the dropdown list can happen without changing the dropdown, hence the onchange event is not a sure way to determine that...
2
4518
by: James | last post by:
Are there any style elements that work on select elements on Apple/IE? I want colored text, colored background, or just about anything colored in a select drop-down but can't seem to make it work on Apple/IE. Thanks, -James
0
4050
by: Francis Parsons | last post by:
Hi! I'm doubtful there's a workaround for this problem, but it's worth a shot. I'm using IE's (css) "zoom" property to zoom the body of a document (in conjunction with persistance so the previously selected percentage level is restored each time the page is reloaded). I don't expect zoomed images to look perfect, but overall the quality of...
2
1942
by: Dave Hammond | last post by:
I'm not sure if this is technically a javascript question, but I'm coding in javascript, so I'll try here first. To overlay a section of a page I typically use an absolute positioned <div> and call insertAdjacentHTML. By raising the z-index higher than than the underlying elements this method works fine, except if there is a window'd...
3
6610
by: JIK | last post by:
Hi, I'm looking for a multi-select dropdown to use in my asp.net/C# app. Anybody have a tip? Jon
1
2949
by: Joe Attardi | last post by:
Hi all, On a form on one of my pages I have two <select> elements, and each one is paired up with a radio button. The idea is to choose an item from one list or the other and select the radio button of the list you want to use. I'm using JavaScript to automatically select the radio button corresponding to a list when you click on the...
1
1875
by: javidride | last post by:
When i render a overlib popup, it appears behind select elements. I would like get a good solution to solve this problem.
17
16999
by: Sam Kong | last post by:
Hello, There are 1 or more select elements with the same name in a form. Let's say that the name of the select is 'select1' and the name of the form is 'form1'. If there's only one select I access it like form1.select1. If there are more than one select, I access them like form1.select1. To check if there are more than one, I thought I...
5
1771
by: mixey | last post by:
Hi gurus, I have following code: <select id="edit-field-color-tids" class="form-select" multiple="multiple" name="field_color"> <option value="362">ffcedc</option> <option value="363">ffdebe</option> <option value="364">fff5c5</option> <option value="365">e1ffd8</option> <option value="366">bff7ff</option> <option...
3
2192
by: ibeehbk | last post by:
Hi. I have a form made in xhtml. I test via vbscript to make sure none of the fields are empty and properly formatted (ie email). All the regular fields work. However, I have two drop down menus that are SELECT elements (javascript-- one named arrivalcity and one named returncity). Basically they ask for a departure and return city and another...
0
7177
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7559
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7542
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5701
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4756
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3248
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3237
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1611
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
811
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.