const min_width = 20; const rpad = 15; const tmp = document.getElementById('js_tmp'); const opts = new Array(document.getElementById('sect'), document.getElementById('arch')); function fix_width(e) { let style = window.getComputedStyle(e, null); let font_sz = parseFloat(style.getPropertyValue('font-size')); tmp.style.fontSize = font_sz + "px"; tmp.innerHTML = e.options[e.selectedIndex].text; let tmp_width = parseFloat(window.getComputedStyle(tmp, null).width); let width = (min_width + tmp_width + (tmp_width > 0 ? rpad : 0)) + "px"; e.style.width = width; } for(let i=0; i < opts.length; i++) { opts[i].addEventListener("change", (ev) => { fix_width(opts[i]); }); fix_width(opts[i]); }