View Full Version : Dynamically set the width of drop down list

01-28-2009, 05:11 PM
How can i set the width of a drop down list dynamically ie. to the length of the selected option.

Thanks in advance.

01-28-2009, 07:18 PM
If you're referring to selects, they automatically size to the width of the largest option (if void of style attributes). This also applies to dynamically inserted options.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<style type="text/css">
<script type="text/javascript">
// <![CDATA[

function addOption()
var sel = document.getElementsByTagName("select")[0];
var new_opt = document.createElement("option");
new_opt.innerHTML = "loooooooooooong";

// ]]>


<br /><br />
<input type="button" value="click me" onclick="addOption()" />


EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum