Friday, August 24, 2012

Create stylish select box in jquery


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
            <title>Stylish Select Box</title>
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
            <script type="text/javascript">
            // JavaScript Document
            jQuery(document).ready(function(){
                // select element styling
                jQuery('select').each(function(){
                    var title = jQuery('option:first', this).text();
                
                    if( jQuery('option:selected', this).val() != ''  ) title = jQuery('option:selected',this).text();
                
                      if(jQuery(this).prev('span').text() == '' && jQuery(this).is(':visible')) {
                        jQuery(this)
                            .css({'z-index':10,'opacity':0,'-khtml-appearance':'none'})
                            .before('<span class="select">'+ title + '</span>')
                            .change(function(){
                                val = jQuery('option:selected',this).text();
                                jQuery(this).prev().text(val);
                            })
                    }
                })
            });
            </script>
            <style type="text/css">
            .option_select_box select{
                color: #57462B;
                cursor: pointer;
                font-size: 12px;
                font-style: italic;
                height: 28px !important;
                line-height: 28px;
                margin: 0;
                position: relative;
                width: 115px !important;
            }
            .selecttest {
                background: url(" http://justexample.netne.net/selectbox/images/select_box6.png ") no-repeat scroll 0 0 transparent;
                border: medium none;
                padding-right: 10px;
            }
            .option_select_box{
                position: relative;
                z-index: 1;
            }
            .option_select_box span{
                background: url(" http://justexample.netne.net/selectbox/images/select_box6.png ") no-repeat scroll 0 0 transparent;
                color: #57462B;
                cursor: default;
                float: left;
                font-size: 12px;
                height: 28px;
                left: 0;
                line-height: 28px;
                overflow: hidden;
                position: absolute;
                text-align: left;
                text-indent: 10px;
                top: 0;
                width: 115px;
                z-index: 5;
            }
            </style>
    </head>
   
    <body>
            <div class="option_select_box">
              <select class="selecttest" name="">
                <option value="1">15 days</option>
                <option value="2">30 days</option>
                <option value="3">60 days</option>
              </select>
            </div>
    </body>
</html>

see the Demo here. 


No comments:

Post a Comment