Last active
August 29, 2015 13:57
-
-
Save fstorr/9795137 to your computer and use it in GitHub Desktop.
Occasionally there's a need to create a fancy-looking dropdown menu that involves showing and hiding all manner of things. This seems to mean the select element doesn't expand to the width of its content. This function loops through the option tags, finds the amount of letters in the longest one, grabs the document's font size, multiples those v…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function sizeSelects(){ | |
| var fs = window.getComputedStyle(document.querySelector("body"),null).getPropertyValue("font-size").slice(0,-2); | |
| var selects = document.querySelectorAll("select"); | |
| for (var select = 0; select < selects.length; select++){ | |
| var opts = selects[select].querySelectorAll("option"); | |
| var len = 0; | |
| var longest = 0; | |
| for(var q=0; q<opts.length; q++){ | |
| len = opts[q].textContent.length; | |
| len > longest ? longest = len : longest = longest; | |
| } | |
| document.querySelectorAll("select")[select].setAttribute("style", "width:" + (fs*longest) + "px"); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment