The code is as below. As per the text size I want to change the width of DIV. Like if the max text length of select option is more than 100 then change the width of parent div to 240px and also change the width of select tag.
<div class="products">
<select>
<option>Test 1</option>
<option>Test 2</option>
<option>Test 3</option>
<option>Test Test Test Test Test </option>
</select>
</div>
javaScript I tried
jQuery(document).ready(function(){
jQuery('.products select option').each(function(){
var maxLength = jQuery(this).val().length;
console.log(maxLength);
});
// if max length is greater than 50 then increase the width of the div 250px
});
Example JS Bin
The rendering mechanism of a select box is, It takes the by default the max Length. Suppose In your example when select box find “Test Test Test Test” select box takes its length.
To achieve what you want you can write following.
Your jQuery:
Your Html Markup would be :-