Skip to content

Commit 969b5ba

Browse files
author
Steve Piron
committed
v1.0.0
Signed-off-by: Steve Piron <steve@basedigital.io>
1 parent a2ac3fe commit 969b5ba

File tree

2 files changed

+87
-1
lines changed

2 files changed

+87
-1
lines changed

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,14 @@
1-
# resize-select
1+
# jQuery auto width select
22
A jQuery plugin that resizes selects to to width of their selected option.
3+
4+
## Settings
5+
Option | Type | Default | Description
6+
------ | ---- | ------- | -----------
7+
iconWidth | integer | 0 | The width of the icon (if any), or more likely the width of the padding over which the icon is applied as background image. Only useful if the `select` isn't set or doesn't inherit of `box-sizing: border-box`
8+
classes | string | false | The classes to be applied on the temporary element to inherit the same font properties as the select, space-separated.
9+
10+
### Dependencies
11+
jQuery
12+
13+
### License
14+
Copyright © Steve Piron

sp.auto-width-select.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**!
2+
* @author Steve Piron <https://twitter.com/stevepiron>
3+
* @requires jQuery
4+
*
5+
* A jQuery plugin that resizes selects to to width of their selected option.
6+
*/
7+
(function( $ ) {
8+
9+
var defaults;
10+
var params;
11+
12+
// ====================================================================== //
13+
// Functions
14+
// ====================================================================== //
15+
16+
/**
17+
* 1. Create a dummy `span` element using the ame text as the selected
18+
* (current) option.
19+
* 2. Append it to the body to get its width, then remove it.
20+
* 3. Resize the original select.
21+
*/
22+
function resizeIt( $select ) {
23+
/* [1] */
24+
var text = $select.find('option:selected').text();
25+
var className = (params.classes) ? ' class="'+params.classes+'"' : '';
26+
var $dummyOption = $('<span'+className+'>').html(text);
27+
28+
/* [2] */
29+
$dummyOption.appendTo('body');
30+
var width = $dummyOption.width();
31+
$dummyOption.remove();
32+
33+
/* [3] */
34+
$select.width(width + params.iconWidth);
35+
}
36+
37+
// ====================================================================== //
38+
// Plugin
39+
// ====================================================================== //
40+
41+
$.fn.spAutoWidthSelect = function( options ) {
42+
43+
/**
44+
* Note: using `return` keeps jQuery's chaining possibility
45+
*/
46+
return this.each(function() {
47+
48+
var $this = $(this);
49+
50+
defaults = {
51+
iconWidth: 0,
52+
classes: false
53+
};
54+
55+
params = $.extend( defaults, options );
56+
57+
// ============================================================== //
58+
// On ready
59+
// ============================================================== //
60+
61+
resizeIt( $this );
62+
63+
// ============================================================== //
64+
// On change
65+
// ============================================================== //
66+
67+
$this.on('change', function(){
68+
resizeIt( $(this) );
69+
});
70+
71+
});
72+
};
73+
74+
}( jQuery ));

0 commit comments

Comments
 (0)