1

Is there any easy way to add a style to a select box in a form using jQuery?

<select>
  <option>sample</option>
  <option>sample1</option>
</select>

Thanks.

rgettman
  • 176,041
  • 30
  • 275
  • 357
jhunlio
  • 2,550
  • 4
  • 26
  • 45
  • 1
    Can't style select boxes, try reading up on `progressive enhancement` .. – painotpi Feb 14 '13 at 04:21
  • 1
    We can style `select` Read http://bavotasan.com/2011/style-select-box-using-only-css/ Another stackoverflow Question http://stackoverflow.com/questions/1895476/how-to-style-a-select-dropdown-with-css-only-without-javascript – asifsid88 Feb 14 '13 at 04:26

6 Answers6

7

You can alternatively use jQuery.SimpleSelect, which is nothing less than a very simple and lightweight (1.75kb gzipped) plugin that will transform your <select> elements into a bunch of divs you'll be able to style however you want using CSS.

It's as simple as that:

HTML

<select>
    <option>Option 1</option>
    <option>Option 2</option>
    <option>Option 3</option>
</select>​

JavaScript

$("select").simpleselect();

And (with default styles that you can obviously customize), it gives you this:

enter image description here

Documentation

user1728278
  • 625
  • 7
  • 13
5

Check out Select2. It will style a select box and can also use Ajax to get data on the fly.

$(selector).select2();
Steve
  • 9,335
  • 10
  • 49
  • 81
2

Try easy styling select box using jquery

  <select class="selectboxdiv">

      <option>choose preset...</option>
      <option>two</option>
      <option>something</option>
      <option>4</option>
      <option>5</option>

  </select>

  <div class="out"></div>

http://fiddle.jshell.net/9Wt89/

Sandip
  • 21
  • 2
0

You cannot style the <select> node but you can visually represent styled <ul> node that it is a dropdown menu. Just When the page loads hide the original dropdown menu and create one by forming a list by <ul> tag. And when the user click on <li> node then change also the value of <select> tag.

Muhammad Talha Akbar
  • 9,952
  • 6
  • 38
  • 62
0

There is no easy way to style form elements in general to be consistent across browsers. But you could go for plugins like Uniform

darshanags
  • 2,519
  • 1
  • 13
  • 19
0

You cannot style the <select> tag. Trying wrapping them around with some HTML and you can style/script your HTMLs as you need.

You can do something like this (in jQuery), wrap your <select> with some markup,

<div class="selectBox">
    <ul>
        <li></li>
        <li></li>
    </ul>
    <select>
        <option></option>
        <option></option>
    </ul>
</div>

And then map <li> changes to <option> changes in your script.

painotpi
  • 6,894
  • 1
  • 37
  • 70