Auto resize select tag element width according to option

Posted By : Priya verma | 28-Sep-2016

Normally the select element tag get its width from the biggest option element. But if you want resize select element tag width resize by selected option value’s .

Html

 

<select id="resizingSelectTag">

      <option>All</option>

       <option>Lorem Ipsum</option>

       <option>Lorem Ipsum is not simply random text.</option>

</select>

<select id="selectTagWidth">

       <option id="widthTempOption"></option>

</select>

 

 
CSS
 
#resizingSelectTag {
 width: 50px;
} 
 
#selectTagWidth{
 display : none;
} 

Jquery

$(document).ready(function() {
 $('#resizingSelectTag').change(function(){
    $("#widthTempOption").html($('#resizingSelectTag option:selected').text());
    $(this).width($("#selectTagWidth").width());  
 });
});

Note: If user select another option, select element width should resize automatically and text is always visible.

 

THANKS

 

About Author

Author Image
Priya verma

Priya is a bright UI designer, having skills in , HTML, CSS, javascript, bootstrap. Her hobbies are playing outdoor games.

Request for Proposal

Name is required

Comment is required

Sending message..