A Nicer Select Dropdown

Better select boxes are all over the internet, but I actually couldn’t find one that I liked, and that worked for what I needed.  Generally when I find code for little elements like this, they are overloaded with stuff.  One of the things I dislike most is where there is a large css file that I also have to include to get the desired behavior.  This version, perhap not as robust as some out there, does away with extra bloat and styling (I’m sure you’re capable of styling it however you want it to look).  Any styles, like the showing and hiding of elements is handled through the javascript so you don’t have to worry about setting any styles for that.

An overview:

  • You can optionally set a data attribute so the dropdown will automatically match the width and margin of whatever element triggers it, like an input box.
  • You don’t have to insert any css to make things show or hide (although the background of the dropdown is transparent by default, so you may want to give your dropdown div a background color at least).
  • While this can just be used as a dropdown for whatever, I used it as a way to create a select box, where you can use checkboxes to select items (or multiple items).  The script looks for checkboxes in the dropdown div.  If it finds that there are checkboxes and the triggering element is an input box, it will automatically update the input box with the list of values as the user checks items.
  • As mentioned, this isn’t specifically set up to be a dropdown selector, so it won’t autobuild the html for the checkboxes in the dropdown.  I think it’s more flexible this way, and that way you can name the checkboxes and get the respective data however you want.

refscript–cmnModules/cmnJs/el_dropdown.js–refscript