0

As usual, option elements are shown when a select element is clicked and I'd like to get the height of the option elements.

Edit:

<select>
    <option>a</option>
    <option>s</option>
</select>
asynts
  • 2,213
  • 2
  • 21
  • 35
Ozgur
  • 1,744
  • 4
  • 17
  • 24

4 Answers4

7

That's not possible, as it's actually not the option elements that are shown.

The HTML standard only specifies that the browser should provide some way of choosing from the options, not how that should be displayed. Therefore there is no standard way of getting any information about how it's displayed.

Regular browsers show the options as some kind of dropdown list, but other browsers may show it in a completely different way. Some mobile phone browsers for example shows a popup that covers the entire screen.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
1

I believe this is what you want

$(document).ready(function () {
  $("#mySelect").change(function () {
    alert($(this.options[this.selectedIndex]).height());
  });
});

Here is a demo http://jsfiddle.net/xf3wD/

John Hartsock
  • 85,422
  • 23
  • 131
  • 146
  • nope i was wrong at my previous (deleted) comment, so it does not work. – Ozgur Nov 21 '10 at 21:47
  • @Ozgur give us some more information and perhaps we could assist you. – John Hartsock Nov 21 '10 at 21:50
  • @Ozgur ... Take a look at the demo I made and let me know what is wrong. – John Hartsock Nov 21 '10 at 22:02
  • @John Hartsokc: Try it in IE. The code reports the size that you specified, eventhough the style is ignored. So, it's not reporting the actual size of the options. – Guffa Nov 21 '10 at 23:28
  • @Guffa, yeah IE will probably behave differently as thier implementation of dropdowns is different than FF. Not sure about this one but I will think about it. – John Hartsock Nov 21 '10 at 23:30
  • @Guffa Actually you may be able to assume the height of the option is the height of the select as IE does not see to support the height on an option tag. – John Hartsock Nov 21 '10 at 23:32
  • @John it seems it is browser depended so other than firefox i may calculate an option height based on font size. Thanks. – Ozgur Nov 22 '10 at 18:01
0

If you're using a library like jquery, you can do:

$('#select option:first').height()

See here.

If you're not using jquery, look at offsetHeight. This should get you what you want.

Jon
  • 3,280
  • 2
  • 15
  • 16
  • Unfortunately this does not work as computed value of an option element is 0 – Ozgur Nov 21 '10 at 21:18
  • I haven't tried jquery, but I'd guess they handle option heights appropriately. Thanks for the tip though did not know that about options. – Jon Nov 21 '10 at 21:18
0

On select you can do that:

$(document).ready(function() {

    $('#select').click(function() {
        var H = $(this).children().height();
        alert(H);
    });

});
Andrea Turri
  • 6,480
  • 7
  • 37
  • 63
  • 1
    click event never is fired on a select element but mousedown. Neither works anyway. Also children is a method not property so it should be children() – Ozgur Nov 22 '10 at 18:00