Niraj Choubey Niraj Choubey - 1 year ago 145
Javascript Question

How to set height of ListBox to auto

I have a Html ListBox:

<select id="targetField" multiple="multiple" name="D1" style="width:200px;">

I need to set the height of this to auto i.e I do not want scrollbar to appear.

I tried
But it is not working in IE.

How should I do this?

Answer Source

Set the size property to the number of items, like this:

<select id="targetField" multiple name="D1" style="width:200px;" size="7">

If you need to do it programmatically, you can set all <select> elements to their option length, like this:

$("select").attr("size", function() { return this.options.length; });

You can test it out here.