According to MVP, achieving the real width of a <select>
element is not possible, but it can be imitated. Here's an approach that demonstrates this:
function adjust_select_width() {
//estimated pixel width of the arrow button in a <select> -
//may vary across browsers
var buffer = 24;
//generate a hidden <span> with the text of the selected option
var temp_span = $('<span>').html($(this).val()).hide();
//add the <span> to the DOM for measurement purposes
$('body').append(temp_span);
//set the width of the select box equal to the width of the <span>,
//plus the buffer for the arrow button
$(this).width($(temp_span).width() + buffer);
//remove the temporary <span>
$(temp_span).remove();
}
//invoke function once to establish initial width
adjust_select_width.call($('#select_id'));
$('#select_id').change(adjust_select_width);
This technique should provide a close approximation to the actual width of the <option>
by creating and measuring an element on the page that houses the desired text.
To illustrate that character width does not impact this method, I used M
(widest English alphabet character) and i
(narrowest).