When creating your web designs, you are always striving for a consistent look across the different browsers. Unfortunately, one of the most fundamental elements of your website - the browser controls - also prove the most difficult to style. Some of them, like the select element, are impossible to change beyond a certain extent. This is why, today we are building a script that is going to take an ordinary select element, and replace it with a better looking, markup powered version, while keeping all the functionality intact.
As usual, we are start with the HTML part of the tutorial. I am using the HTML5 markup as it gives us some useful features, such as the data attributes, with which we can add arbitrary data to the markup of the page.
You can see that we are using the data attributes to embed information in the option elements of the select. We are including a product icon and a rich text description, both of which are later displayed in the improved version of the select element. I've set an arbitrary data-skip attribute on the first element, so that our script knows not to include it in the generated list.
You could alternatively just check for the existence of the data-icon and data-html-text attributes and skip the element if necessary.
At the bottom of the document are included version 1. As you can see, we are basically constructing an unordered list, with a li element representing each option of the select. The select box itself is represented by a div with a. On page load, the script scans through the options of the select element, and generates the markup according to the HTML5 data attributes that these items contain.
As of jQuery 1. This is a really handy feature, which makes it really easy to read the embedded data. The original select element is not destroyed - it is only hidden with the hide method. This is important, because as you can see from the code above, we are reflecting any changes of the selection back to that original select element. This way, when you use the select as part of a form, the values will be properly recorded and passed to your backend script. Now that we have our code in place, lets take a closer look at the CSS3 magic that makes it all possible.
As you can see from the markup at the top of the previous step, we are only using a minimal amount of markup to display the select box and the drop down. If we were confined to use pre-CSS3 techniques, we would have to add significantly more divs and spans.
CSS3 allows us to assign multiple background images to elements by just adding multiple url declarations divided by a comma. They are added to the element from top to bottom, with each consecutive background displayed below the previous. Currently multiple backgrounds are supported by Firefox, Safari, Chrome and Opera. For Internet Explorer and older versions of the first browsers, a fallback is defined, which is basically just a regular version of the background.
When parsing the CSS document, browsers that do not understand multiple background will just ignore the rule and use the plain one.
The box-sizing property that I've used for the. Normally, the borders here would increase the overall width with 2px and ruin the alignment. With box-sizing set to border-boxhowever, the overall width will not exceed the one specified in the definition and borders will take up space on the inside. In this tutorial we demonstrated some of the handy features that were introduced with jQuery 1.
A good thing about this script, is that it keeps the original select box hidden on the page, and changes its value according to the fancy replacement. This way, when you submit the form the correct value is also passed along.
Great work - unfortunately there seems to be a problem using later versions of the jquery lib jquery Awesome will definitely use this with twtnotes! Thanks for the suggestion! I added a CSS declaration in. I also updated the article and the download. I am not an expert but it is not working with different option values! That's some pretty big functionality that has been lost here.
I should change it to "most of the functionality intact". Other than that, keyboard navigation should probably be included in version 2. The only issue I see is that you've used the html5 data feature to put style back into the HTML document.
You are defining the icon for each option, an essential part of style, within the HTML doc. I haven't really thought about it any further yet, but naturally, there's a way you could instead use an ID for each option and assign a CSS-defined image for each option using it's ID. And you could use the 'rel' tag within the option to define the text. Doing so also wouldn't require the use of HTML5, allowing for wider adoption and greater platform conformity.
Again, apart from this one aspect, I think the overall technique and end result are great! I always try to keep my markup to a minimum, and decided it was a good place to demonstrate jQuery's new data method enhancements. I also don't think that option elements are supposed to contain anything other than plain text.
You can think of it this way: This has blown my mind, this opens up some fantastic opportunities to enhance web forms appearance. I have tried adding multiple select boxes to a page, and any more than just the one fails to reveal the drop down on click.
I was expecting that the class could be added to any select input that has the correct data attributes. Any idea why this is? Digging a little deeper, it looks as though all of the options from each select is appended to every ul in the drop down and the show and hide functionality is prevented from working when there are multiples.
Yes, encapsulation would be a good enhancement to the script. I will tackle it during the weekend and turn it into a jQuery plugin. Is there a chance you've turned this into a plugin? Or, at the very least, have to create a script that allows for multiple selects on the same page? Have you found a way to make this successful.
I was wondering if we could create the drop down selection boxes to sit side by side forex lhdn of on top of other one. Nice CSS element application! I'll use it singapore companies market capitalisation my project!
Thanks and keep on your work! I recently found your site. The tutorials that you write are simply awesome and I really appreciate the fact that you explain everything in detail. I have remove option select jquery to other similar sites too and I can tell you, I have never seen any site that explains like you do.
Just wondering, it's possible to drop down the menu on mouse hover instead of mouse click Srry about my english, I'm still learning. I added it to some forms, but I had to edit a few things.
First of all, I do not need the image order data features and the width of the div was wrong because of some paddings inside the form. Then I want to use the keyboard, so I changed the hiding of the the select field. This is still not ideal, because the dropdown does not scroll to the actual entry, but you can now use the tabkeys to jump to the selectfield and change it with the keyboard. There is another technique to add better selectfields, but it uses the whole jquery ui lib and I do not want to load 40 kb of code, that is unused on the rest of the page.
Please remove the textshadow on focus mars stock market symbol for gold price this textarea.
If you select a part of the text, it is really unreadable. Oh, and I do not use the multiple background, but gradients and a em element, that is shown as a little arrow.
Looks nice and does not need any images. I ran into a small problem when more than one select box is present on one and the same page. The drop down function isn't working. I get the styling working, but when I click on the the select box, the drop down list won't show up. Is it possible to make multiple selects work with this script?
But i'd like to know if we want when we click an option instead of showing in select dropdown to take the value of the option and move on a link, how this could be added as function in script file?!!
For example if you want when you choose iPod to link to iPod's website how could be done?? But I see that when you click the first option, do nothing and if you click it second time it moves to the location you define. Hi Martin, Very nice works from you, but I am having a same problem with Giorgos, which i can't add in a link to the value.
Event types of "change" don't work. The solution of the following: Need to add the following line of code. In my case I have two different dropdown top ten forex robot 2013 areas in the same page. I named them with different classname on the script file.
One is the regularSelect and I don't want the. The other one 'mysort' I need the. Applies the trigger 777 how to win in binary options zone both. Is there a way to stop the.
I found the solution to having two dropdown menus in the same page with different functions. One has a link to the value. You can modify the html to fit your needs. Replace mynewclass with you class name. This should be included in the script. Thanks for the tipp, I needed that, and wouldn't have found it else: Apparently they don't use className anymore. But if you change that to class, everything is solved.
I hope I saved someones time with this. Thanks for the custom drop down solution and nice code. I am developing the website for "iPhone, iPad and iPod".
Also the custom drop down with google map is working fine in system browser like Firefox, chrome Thanks for this tutorial!!
Is gbp usd exchange rate yahoo possible to load a cufon font in my dropdown? I don't get at what point I should do the cufon font replacement, if it's even possible. I was wondering is there is a way to allow the use of two or more select objects using this code on the same page.
I have tried to add an additional select element within the html and when i do so it losses funcationality. When I updated the jquery to latest jquery Yes, facing the same issue. It's because of the variable names in the library, need to catch those and replace with old ones, but no luck yet. Hey, Thanks for the wonderful tutorial. However m stuck in a problem.
I've edited the css as per my requirement, but somehow there is blank space on left side assigned for images I guess which is just not ready to go.
Martin thanks a lot for the Tutorial! Ross thanks a lot for the advise. Got crazy about why it does not work, but no everything is fine Martin thanks for the code but how to connect the links that I want the choice of one shift on a link to another page. I've having trouble with the drop down li not being on top of other elements on the page. I've tried adding z-index to no avail. It's kinda hard to select an option when it is under other select boxes.
I was asking a coworker the other day, "how would you do a select with custom HTML in it? Radio buttons are exactly that - they let you make a single selection from a group of choices, and a single value is generated when the form is submitted, so in data model terms, they are for all intents and purposes the same.
Without a bunch of JS to parse and transform elements into something else, such as implemented by a bunch of popular jQuery plugins, and elements are readily stylable:. Just a very simple proof on concept, but I think the idea makes more sense than trying to warp elements into something they're not - radio buttons are a much closer match when you need custom markup, I think. Making Better Select Elements with jQuery and CSS3 November 6th Per popular demand, this script is now available as a stand-alone jQuery plugin.
Read more and download the code at our Converting jQuery Code into a Plugin tutorial. The HTML As usual, we are start with the HTML part of the tutorial. The jQuery On the document. Now lets take a closer look at how this code is generated.
The CSS As you can see from the markup at the top of the previous step, we are only using a minimal amount of markup to display the select box and the drop down. With this our jQuery and CSS3 powered select box is complete! Parting Words In this tutorial we demonstrated some of the handy features that were introduced with jQuery 1. Presenting Bootstrap Studio a revolutionary tool that developers and designers use to create beautiful interfaces using the Bootstrap Framework.
Photobooth with PHP, jQuery and CSS3. Making an Impressive Product Showcase with CSS3. Shiny Knob Control with jQuery and CSS3. Better Check Boxes with jQuery and CSS.
Add and Remove Multiple or Single Select Options using jQuery
This discussion is closed. Tristan 6 years ago. This is very awesome! I wonder what you have in store for us next week James Woods 3 years ago. Zach 6 years ago. Vincenzo Ferme 6 years ago. Martin Angelov 6 years ago.
I updated the article and the download. Sebastian 6 years ago. I don't think I can find 1 mediocre tutorial on this site - all are awesome! Thank you for the awesome comments folks! I have to admit, the tuts on this site are always presented as beautiful as the content.
Love your work, style, simplistic approach and range. Joey 6 years ago. Jacob Raccuia 6 years ago. Alex 6 years ago. Another amazing tutorial, thanks a lot for this! Codeforest 6 years ago. Martin, great article as always.
Brendon Kozlowski 6 years ago. It even surprising to me passed the "will it work in IE9" test! Matt 6 years ago. Now do it with 0 images and I'll really be impressed! Matt Stow 6 years ago. Poster Nutbag 6 years ago. Thank you for the comment. You are, however, free to modify the script as you see fit. Kevin Rapley 6 years ago.
I cannot wait to see how you handle radio buttons and check boxes! Macklin Chaffee 6 years ago. Thanks for your efforts on this, saved me a bunch of time. Kailash Gyawali 5 years ago. Any updates on making it work multiple select in one page? Jenna 4 years ago. Salas 6 years ago. SamsonDelila 6 years ago. Mezanul 6 years ago. Hello Martin, I recently found your site. Kudos for your nice work.
Shay 6 years ago. Is it possible to add a link to them?. Daniel Gileta 6 years ago. Beben Koben 6 years ago. Oliver 6 years ago. Anyway we can add a link to them? Maicon Sobczak 6 years ago. Amazin utilization of CSS3 and jQuery. This tutorial showed me that I have so much to learn Chandan Chakraborty 6 years ago.
Daniel S 6 years ago. Awesome Work and great explained! Igor Klajo 6 years ago. Nice and functional script, thanks for the share. Or is it working and I'm doing something wrong. Giorgos 6 years ago. Very nice and useful tutorial!! Martin, I tried to add in script file in: How could be fix this and link to the location in first click??!! Kaven 5 years ago. Need to add the following line of code select.
And thanks for this plugin: Bugsy 5 years ago. Thanks for the solution but I have one question. Tomas Misura 5 years ago. Dego 5 years ago. Hello, i made a Onchange function to my select, but dosen't work. Waut 5 years ago. I was trying this with the latest jquery-library and it didn't work.
But I found the solution. Li77leMowgli 5 years ago. Thanks a lot, you saved me a lotta time ; It works with jQuery 1. RakeshSoni 5 years ago.
Socrates 5 years ago. Wish this would work with jquery 1. Charles Robbs 5 years ago. Vancouver Wordpress 5 years ago. John 5 years ago.
Nice job Martin, great explanation, tested in ie8, ie9, chrome, firefox and safari latest versions, works great on all. Josh 4 years ago. Linas 4 years ago.
Rob 4 years ago. Just wondering how to make each of the list items a link to an external page? Rasmus Schultz 2 years ago. Without a bunch of JS to parse and transform elements into something else, such as implemented by a bunch of popular jQuery plugins, and elements are readily stylable: Interested in development and design. Busy working on the next big thing. Tutorialzine is a community of talented developers who learn together. Sign up for Free.
PHP Time Between Dates. Anyone know of a good react starter kit? HTML email 2-coulmn layout.