Last Updated: February 25, 2016
·
2.751K
· briandillingham

Select box reset button with Jquery and CSS

Here is a simple method for creating a reset button on select boxes without displaying the select box. Why? I've seen methods of placing a cancel button beside the select but I feel it's not as clean as it could be. The default method would be to click the select box and select it's default value, but that 1 too many steps in my opinion. So here it goes.

View Demo


HTML

<select>
    <option value="">Select a color..</option>
    <option value="red">Red</option>
    <option value="green">Green</option>
    <option value="blue">Blue</option>
</select>

<div class="displaySelect">
    <span class="value"></span>
    <span class="close">⊗</span>
</div>

CSS

.displaySelect{
    display:none;
    border: 1px solid;
}
select, .displaySelect {
    text-indent:20px;
    font-family:helvetica;
}
select, .displaySelect{
    font-size:22px;
    height:50px;
    line-height:50px;
    width:100%;
    text-transform:capitalize;
}
.displaySelect .close{
    display:block;
    float:right;
    width:10%;
    text-align:center;
    font-size:52px;
    cursor:pointer;
}

Jquery

var select        = $('select');
var selectResults = $('.displaySelect');
var selectValue   = $('.value', selectResults);
var selectClose   = $('.close', selectResults);

select.on('change', function() {
    $(this).add(selectResults).toggle();
    selectValue.html(this.value);
});

selectClose.click(function(){
    select.val('').fadeIn();
    selectResults.toggle();
    selectValue.html('');
});