...

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



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

Thanks in advance.

itsallkizza
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.

Example:


<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Example</title>
<style type="text/css">
</style>
<script type="text/javascript">
// <![CDATA[

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

// ]]>
</script>
</head>
<body>

<select>
<option>short</option>
</select>

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

</body>
</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum