needed a way to force redraw of `](https://github.com/h5bp/lazyweb-requests/issues/92), recommending Select 2 (an improvement on Chosen). – Barney Aug 21 '13 at 12:50

  • So this question is effectively a duplicate of many questions that has been asked before – Huangism Aug 21 '13 at 13:05
  • 2

    (Note: Please ignore this answer as your edit suggest not to use clone. If anybody face this problem and want to go for cloning use this, which is the reason I am not removing my answer).

    If you can manage to put a clone to select tag even though events are attached as desired result is almost not possible with current implementation. Here is the approach with code.

    Browser's checked: Chrome 28.0.0 Firefox 22.0 IE 7.0/8.0

    1. Used a clone select for managing the desired result.

    CODE:

    !DOCTYPE html>
    <html>
    <head>
      <meta http-equiv="content-type" content="text/html; charset=UTF-8">
      <title> - jsFiddle demo</title>
    
      <script type='text/javascript' src='/js/lib/dummy.js'></script> 
    
      <link rel="stylesheet" type="text/css" href="/css/result-light.css">
    
      <style type='text/css'>
      </style>    
    
    <script type='text/javascript'>
    
    window.onload=function(){
    var select;
    
    function webkitFix(){
        document.body.removeChild(select);
        setTimeout(function(){ document.body.appendChild(select); }, 1);
    }
    
    setTimeout(function(){
        select = document.getElementsByTagName("select")[0];        
        select.style.fontSize='40px';       
        webkitFix();
    
    
        setTimeout(function(){
        select.style.visibility = 'hidden';
        select1 = document.getElementsByTagName("select")[0];
        select1.style.visibility = 'visible';    
        select1.style.fontSize='10px';
        webkitFix();
        },5000);
    
    },5000);
    }     
    </script>  
    
    </head>
    <body>
      <select id="one" multiple size="6" style="font-size:10px;">
        <option value="AAAAAA1">AAAAAA1</option>
        <option value="BBBBBB2">BBBBBB2</option>
        <option>AAAAAA3</option>
        <option>BBBBBB4</option>
        <option>AAAAAA5</option>
        <option>BBBBBB6</option>
        <option>AAAAAA7</option>
        <option>BBBBBB8</option>
        <option>AAAAAA9</option>
    </select>
    
    <select id="two" multiple size="6" style="font-size:10px; position:absolute; visibility:hidden;">
        <option value="AAAAAA1">AAAAAA1</option>
        <option value="BBBBBB2">BBBBBB2</option>
        <option>AAAAAA3</option>
        <option>BBBBBB4</option>
        <option>AAAAAA5</option>
        <option>BBBBBB6</option>
        <option>AAAAAA7</option>
        <option>BBBBBB8</option>
        <option>AAAAAA9</option>
    </select>      
    </body>  
    

    Screenshot 1:

    enter image description here

    Screenshot 2:

    enter image description here

    Screenshot 3:

    enter image description here

    halfer
    • 19,824
    • 17
    • 99
    • 186
    Akki619
    • 2,386
    • 6
    • 26
    • 56
    2

    I've tried so many things, height, line-height, offsetHeight, padding, margin, word-spacing, letter-spacing, white-space, text-indent, -ms-word-break, -ms-word-wrap, line-break, and many others.. even tried to understand the IE hasLayout property and how to do some workaround, this bug/issue of IE8 seems to be unfixable..

    If you don't want to use a plugin like what @DavidBarker suggested, neither you want to clone/copy the original dropdown like what @Akki619, here is the best thing I could come up with:

    if($isIE8) {// alternative solution for IE8
    
    
        setTimeout(function() {
            var w = parseInt(select.style.width, 10);
            var h = parseInt(select.style.height, 10);
            // change select zoom rather than changing the font-size (since font-size is buggy)
            //select.style.fontSize = '40px';
            select.style.zoom = '400%';// almost equal to '40px'
            var longestOptionText = 1;
            // for the below, I used jquery to get the longest option text, you might replace it with some javascript
            $('select option').each(function() {
                var txtlen = $(this).text().length;
                if(txtlen > longestOptionText) {
                    longestOptionText = txtlen;
                }
            });
            // -------- end of jquery usage
            select.style.width = (parseInt(select.style.fontSize, 10) * longestOptionText) + 'px';
            select.style.height = (parseInt(select.style.fontSize, 10) * longestOptionText) + 'px';
            setTimeout(function() {
                //select.style.fontSize = '10px';
                select.style.zoom = '100%';
                select.style.width = w + 'px';
                select.style.height = h + 'px';
            }, 5000);
        }, 5000);
    
    
    } else {// all other browsers
    
        // keep your original code here..
    
    }
    

    The result will look kind of similar to what you're expecting, but now there is slightly little problem, if zoom is set to bigger than 100%, the scroller vertical bar will not be visible. Solution: increase the select height to have all options visible.

    evilReiko
    • 19,501
    • 24
    • 86
    • 102
    0

    Try accessing the offsetHeight on the select. It forces a redraw.

    probablyup
    • 7,636
    • 1
    • 27
    • 40
    • 1
      And by accessing, you mean reading? Because you can't write `offsetHeight` – huysentruitw Aug 18 '13 at 07:03
    • 1
      Reading, yes. Accessing doesn't necessarily mean writing. – probablyup Aug 19 '13 at 15:14
    • +1 no need to vote down since this is though. anyway didn't work :( –  Aug 20 '13 at 04:43
    • you can adjust zoom:1 after setting font-size. let me know if it works – Najam Awan Aug 20 '13 at 07:54
    • If you do a simple Google, you'll see there are hundreds of results recommending reading offsetHeight, as the browser has to redraw to calculate that value. Stop being so elitist, it's very unattractive. – probablyup Aug 20 '13 at 15:34
    • 2
      @ultraviol3tlux I did not have the intention to be elitist. But, if you tested this before answering, you would have seen that it didn't work, alas... – huysentruitw Aug 21 '13 at 06:14