Drupal Tutorials

"Fancy" select menus in Drupal using jQuery

The tutorial contains a tiny bit of theme function overriding and Javascript code so you should be comfortable with both and basic theming.

Drupal 7 version If you’re looking for a Drupal 7 version of this please check out the jQuery UI Selectmenu module: http://drupal.org/project/selectmenu.

I recently had to implement fancy select menus while helping out on the redesign of All-X.ca for my good friend and designer, Alex Ventpap.
Alex is very focused on design (as designers are) so while I thought the style of select menus wasn’t so important he had other ideas.

In this quick tutorial I’m going to show you how to create “fancy” select menus in Drupal using the Jquery Dropdown module, a technique which is becoming more popular on the web.

Before we go any further I want to clarify when I mention “select menu” or “drop down menu”, I mean a select menu/element you find in a form such as on Views exposed filters or on webforms rather than navigational drop downs like the “Learn Drupal” menu item here on DrupalSN. Although as you’ll see the principle is quite similar.

An Example

Using All-X.ca as an example I’m going to show you how to change those dull looking select menus in a Views exposed filter form into fancy eye-catching ones thanks to the power of jQuery!

From this:
Dropdown Before

To this:
Dropdown After

How it works?

If you’ve ever tried to style a select element with CSS you’ll know it’s pretty rigid and only supports basic style changes.
So the basic principle of using jQuery is it takes the select menu element and it’s values and change it into a simple unordered list (ul) which allows for far greater styling with CSS.

This:

<select id="edit-type" class="form-select" name="type">
  <option selected="selected" value="All">- Any -</option>
  <option value="5">Branding</option>
  <option value="4">Illustration</option>
  <option value="3">Interactive Design</option>
  <option value="6">X-perience Lab</option>
</select>

plus a little jQuery becomes:

<div class="jquery_dropdown_container jquery_dropdown_type">
  <div class="jquery_dropdown_header jquery_dropdown_header_type">- Any -</div>
  <ul class="jquery_dropdown_list jquery_dropdown_jump" style="display: none;">
    <li><a rel="All" class="edit-type" href="#">&nbsp;Any&nbsp;</a></li>
    <li><a rel="5" class="edit-type" href="#">Branding</a></li>
    <li><a rel="4" class="edit-type" href="#">Illustration</a></li>
    <li><a rel="3" class="edit-type" href="#">Interactive&nbsp;Design</a></li>
    <li><a rel="6" class="edit-type" href="#">X-perience&nbsp;Lab</a></li>
  </ul>
</div>

There are a few jQuery plugins which offer this or similar functionality:

And although these looked great I wanted something already integrated into Drupal so after a bit of searching around I found the Jquery Dropdown module which despite the confusing name (Jquery Select Menus might have been better) looked perfect for the job.

I also came across the Form Beautifier module which also offers some Jquery styled form manipulation but it looked a bit overkill for my needs so I went for Jquery Dropdown instead.

Setting up the Jquery Dropdown module

The Jquery Dropdown module actually contains two modules so first thing is to install and enable both modules in the standard way.

Jquery Dropdown Modules

The second module in the bundle, Jquery Dropdown Jump makes the select menus load the new page without needing to click a “submit” button.

I’ve assumed you’ve set up a View with an exposed form select menu in it that you can use as an example.

Module installed, we’re done right?!..........Wait a minute it hasn’t done anything, wow great module! Wait a second! Before you go uninstalling the module take another look at what the Jquery Dropdown project page says:
bq. The intent behind this module is to give developer’s a “configure-less” way to instantly transform any dropdown to a jquery dropdown by adding the class “jquery_dropdown.”

So we need to put our developer hats on briefly to get the module working.

Hooking up the Jquery Dropdown Module

The way the module works is it scans pages using Jquery for any select elements with the class “jquery_dropdown” and then converts them into unordered lists (ul) instead. So we need to find a way to attach the “jquery_dropdown” class (and “jquery_dropdown_jump” class for the Jquery Dropdown Jump module) to our select menus.

There are 2 ways to add the classes to our select menus (I recommend option 1):

Option 1) A drop more Jquery (Javascript)

Your best to get the Jquery Dropdown module working is to add a bit of Jquery code to your theme that will transcends through your HTML to find the select elements/tags and apply the “jquery_dropdown jquery_dropdown_jump” classes to them.

Attach a new Javascript file
To keep things clean we’ll attach a new external Javascript file to our theme rather than using inline Javascript.

  1. Open up your themes .info (ie. garland.info) and add the following line:

scripts[] = theme.js after any stylesheets definitions.
This just tells Drupal we want to attach the file theme.js when our theme is loaded.

  1. Now create a new Javascript file, call it theme.js and save it to your themes root folder (ie. sites/default/themes/THEMENAME/)
  2. Navigate to Administer › Site configuration > Performance and refresh your data cache.
  1. View the page source or use Firebug to check the new theme.js file is being loaded into the page.

The Javascript Code
Next open up the new theme.js file in your favourite HTML editor and add the following code:

/**
* attach Jquery Dropdown functionality
*/
Drupal.behaviors.themeAttachJQDropDown = function(context) {
  $('select').each(function () {
    $(this).addClass('jquery_dropdown jquery_dropdown_jump');
  });
}

What this does it looks for “each” select HTML element and then adds the “jquery_dropdown jquery_dropdown_jump” classes to them, simple.
If you want to target just select menus with Views exposed filter forms change $('select').each(function () { to $('.view-filters select').each(function () {

Refresh your Views page again and you should see the select menu has changed into an unordered list.

Option 2) Drupal theme function override (PHP)

You could also add the Jquery Dropdown HTML classes to select menus using PHP to override Drupal’s theme_select function.

Add the following to your themes template.php file, change the YOURTHEMENAME to match the name of your theme ie. garland_select:

<?php
function YOURTHEMESNAME_select($element) {
 
$select = '';
  if (
in_array($element['#post']['form_id'], 'views_exposed_form')) {
   
$element['#attributes'] = array('class' => 'jquery_dropdown jquery_dropdown_jump');
  }
 
$size = $element['#size'] ? ' size="'. $element['#size'] .'"' : '';
 
_form_set_class($element, array('form-select'));
 
$multiple = $element['#multiple'];
  return
theme('form_element', $element, '<select name="'. $element['#name'] .''. ($multiple ? '[]' : '') .'"'. ($multiple ? ' multiple="multiple" ' : '') . drupal_attributes($element['#attributes']) .' id="'. $element['#id'] .'" '. $size .'>'. form_select_options($element) .'</select>');
}
?>

Refresh your data cache within Drupal by navigating to Administer › Site configuration > Performance and you’re done!

Both options work well but the Javascript option (number 1) offers a bit more flexibility.

Add a bit of CSS

Our new select menus still look quite dull and a bit of a mess:

Before the CSS

BUT the advantage we have now is it’s now a unordered list rather than a select menu which gives us more freedom to style it with CSS.

Let’s hide the labels & submit buttons:

.views-exposed-form label,
.jquery_dropdown_page .views-exposed-form .form-submit {
  display: none;
}

Now fix the width & height of the list and list items as well as add a background image for the top list item so it looks more like a list:

.jquery_dropdown_container {
  width: 175px;
  z-index: 1;
  position: relative;
}
div.jquery_dropdown_header  {
  background:url(../images/jquery_dropdown/jquery_dropdown-header.png) left top no-repeat;
  border: 0;
  font-size: 13px;
  width: 165px;
  padding-left: 10px;
  font-weight:bold;
  height:32px;
  line-height:32px;
  overflow:hidden;
  text-indent:2px;
  color: #9EC700;
}
ul.jquery_dropdown_list {
  border: 0;
  background: #050702;
  left:0;
  margin:0;
  padding:0;
  position:absolute;
  top:32px;
  width:175px;
  z-index: 1000 !important;;
}

ul.jquery_dropdown_list li {
  background: none;
  border-bottom: 0;
  list-style:none outside none;
  margin:0;
  padding: 0;
}

Finally let’s add some style to the links (<a> tags) and a hover state:

ul.jquery_dropdown_list li a,
ul.jquery_dropdown_list li a:link,
ul.jquery_dropdown_list li a:visited {
  color: #648304;
  display:block;
  font-size: 13px;
  text-decoration:none;
  width: auto;
  padding: 0 10px;
  height: 26px;
  line-height: 26px;
  font-weight: normal;
}
ul.jquery_dropdown_list li a:hover {
  background:url(../images/jquery_dropdown/jquery_dropdown-link-bg.png) left top repeat-x;
  color: #EDE8CF;
}

NOTE: You will have to use your own background images this is just an example of how you could style it.

Dropdown After

And that’s it, thanks for reading! Comment below if you have any questions or problems.

Commenting on this Tutorial is closed.

Categories:

Comments (61)

Thank you so much for sharing this! I am completely new to drupal, so this site is great for beginners like me!

Thank you for your awesome tutorial.

Hoc bong du hoc
Du hoc Singapore

Animal substance seems to be the first food of all birds, even the granivorous tribes. u can see sailboats for sale

Don’t get me wrong, I think bikes are terrific. I own several of my own, including a trendy mountain style, and ride them for pleasure and light exercise. interesting bikes bikes for sale

Another thing I like to do is sit back and take in nature. To look at the birds, listen to their singing, go hiking, camping and jogging and running, walking along the beach, playing games and sometimes being alone with the great outdoors. It’s very special to me. u can see
parrots for sale

This is very useful to many readers like me. Being a student, I am requiring myself to read articles more often and your writing just caught my interest.OSB Board

Tell me what you thought about when you Hughes Koscheck Fight video were gone And so alone The worst is over UFC 135 Stream You can have the best of me. We got older But we’re still Hughes Koscheck Fight video young We never grew out of this feeling that we wont give up. | UFC 136 Live Stream

By the way, adding video will make the tutorial to be perfect.

Du hoc
Du hoc My

I’ve learned a lot from your blog here, Keep on going, my friend, I will keep an eye on itI am very happy to read such a useful blog. Thanks for it and keep it up
music cd

I’ve learned a lot from your blog here, Keep on going, my friend, I will keep an eye on itI am very happy to read such a useful blog. Thanks for it and keep it up
music cd

I’ve learned a lot from your blog here, Keep on going, my friend, I will keep an eye on itI am very happy to read such a useful blog. Thanks for it and keep it up
music cd

The food was missing that kick, some seasoning or spice, to really bring it on home. Conceptually, the dishes sound great, however the execution isn’t the best. (Haha, I realized I sound like Tom Collichio on Top Chef!) Tian Yi

Great, i thinks so

Bag Factory

I have followed all the steps, every thing is very well explained. But I have a problem. The “any” option is only showing at the first time, then, when I change to another option, “any” option disappears. Actually, the option is in the code, but without text, so it isn’t visible. Could you help me?
Thanks!

Thanks for the great tutorial, i’m really enjoying the series.

I’m experiencing a couple of problems.
1) The select list loses any of my groups, ie i’m listing a bunch of counties in the UK and use groups to delineate regions of the country. These are missing.
2) I have a webform with 3 select lists , one after another. Only 2 of the 3 lists expands properly. The middle one just won’t open
3) The z-index is working a bit strangely. My first select list appears underneath my second list?

I may post up over in the issue queue for the module, but it would be good to hear if you’ve had any success with multiple select lists on one page.

Many thanks!

AntonD's picture

A big thank you for sharing this tutorial, I’m not very familiar with jQuery but I guess this gives me a chance to look into it. All hints are helpful.

Andon @ Jogos

Wonderful post! This is very useful to many readers like me. Being a student, I am requiring myself to read articles more often and your writing just caught my interest. Thank you so much.
Diamond Jewelry | Engagement Ring Mountings

Like you I love a good drupal hack, great to see easy to follow tutorials on your blog. Thanks.food processor reviews

williamfitzstephen's picture

Have you Considered Adding videos to your blog readers to keep the more entertained or learned? I just read-through the post and It Was quite good … thanks for sharing your valuable informaion with your blog readers…
Help With Essay|Authentic Essays

I agree! videos would be a very cool addition to this site! Along with the great content, it would help the readers understand the content much better. Looking forward to this feature being added soon clinical research

Awesome Tutorial! I have one question though…
The select box in my theme seems to suddenly change back to the old stale select box once a selection is chosen.

The Jquery comes back but seems to shift back and forth after the selection is made and the page refreshes with the results. Any ideas?

People like you make this Drupal community awesome!

I recently had to implement fancy select menus while helping out on the redesign of for my good friend and designer, Alex Ventpap.\Alex is very focused on design (as designers are) so while I thought the style of select menus wasn’t so important he had other ideas.
—————-
william
IT Specialist
mcdst certification ll mcitp

Thank you for the post. I almost passed your post up in Bing but now I’m glad I decided to stop and got to browse through it. I’m definitely more informed now. I’ll share yours with some other guys I know. LOL.
how to fix advapi32.dll error

This is very useful to many readers like me. Being a student, I am requiring myself to read articles more often and your writing just caught my interest.
Torrent downloads

thanks for this very useful post. I have been try it and it works well with a few items, but not with many items. Is there any easy way for setting a maximum height to dropdown list?

thanks

miranta's picture

i m new here.nice to meet you ,nery good post!!!

Torrents

Your site always has something new on it I hadn’t thought of before! Great work! art

Its one of the good platform for awareness of people. Keep sharing such stuff in the future too.
Angel Flirt

Great canvas prints stuff as always!

Hi, I’m new here and I hope to learn alot about Drupal.

computer repairs braintree

Great stuff from you, man. Ive read your stuff before and youre just too awesome. I love what youve got here, love what youre saying and the way you say it. You make it entertaining and you still manage to keep it smart. I cant wait to read more from you.
http://www.citysmart.ro

Great tutorial. Thanks for sharing. Canvas prints

The methods you described above would help me to retrieve the alias of the page, but that isn’t sufficient to pass to views to process and retrieve the related information for me.
Wholesale makeup

regalos originales
Great new posts, thanks for sharing anniversary gifts.

I’m quite new to jquery – does it work consistently across all modern browsers? It seems to have lots of nice things going for it but as with css3 it needs to work across all browsers without issues.
acrylic art prints

I would like to thank you for the efforts you have made in writing this article. I am hoping the same best work from you in the future as Mynet well.

Wow, nice post,
there are many person searching about that now they will find enough resources by your post
Bit Now

Great post. One minor detail though.

/**

  • attach Jquery Dropdown functionality

*/
Drupal.behaviors.themeAttachJQDropDown = function(context) {
$(‘select’).each(function () {
$(this).addClass(‘jquery_dropdown jquery_dropdown_jump’);
});
}

Should really be

/**

  • attach Jquery Dropdown functionality

*/
Drupal.behaviors.themeAttachJQDropDown = function(context) {
$(‘select’).each(function () {
$(this).addClass(‘jquery-dropdown jquery-dropdown-jump’);
});
}

Just for consistency ….

This is my first time i visit here. I discovered a lot of interesting things within your blog especially its discussion.Running // Tennis Shoes

YoukinellY's picture

I have to say that I really enjoy the all-x.ca redesign and I’ll try to recreate some features in my web design project and also thanks a lot for the tutorial.

thanks you very good
شات الحبشات بنات مصردردشة مصريةدردشة الحب

very nice
thanks

I found it really informational and useful.Thanks a lot foe sharing such an interesting and meaningful article. 11g latest dumps 156-215.70 exam dumps 199-01 free braindumps 1y0-a05 latest dumps latest 1y0-a08 dumps 1z0-047 latest dumps 1z0-050 latest dumps 1z0-051 brain dumps

Good post. Very useful information. I learnt a lot. Thanks
android tablet
wenger backpack

Interesting post, pretty much covered it all for me, thanks.
Originelle Weihnachtsgeschenke

Great Drupal tutorial as usual, thanks! Regards from Buenos Aires, Argentina

Hide the labels & submit buttons are great, thanks for your sharing!
From: Dich vu seo ; web tron goi ; web gia re

no i don’t think so that there is a need to hide the labels. keep moving with this dear
kitchen towels | dish cloths | tea towels | hand paper towels | double towel bar

very interesting.
d&g glasses

I have been using Drupal as a beginner for a while now, and have come to find that I was doing everything the hard way (editing code directly to make modifications).
jump manual
extreme fat loss

this is what i was looking for. Thanks a lot for this jquery. semi formal dress shops || smart casual dress || kurtis || new pakistani dresses || new dresses

Levin's picture

Hohoho.. That’s how it works… I’ll know, thanks!
Kris from iphone application development

Im just started to learn Drupal. Been using wordpress until now.

after completing read articles in some pages here. Ive got a little knowledge and idea about drupal.

thanks.

ereader