solarisuser Posted May 4, 2007 Share Posted May 4, 2007 Hi All, Is there a way to use JS to limit the size of a <select> dropdown menu? An example were I'd like to have the first <option> be display up to 10 characters, but of course the value should remain the entire length. Thanks for any help! <html> <body> <form action=""> <select name="cars"> <option value="volvo Extra Extra Long">Volvo Extra Extra Long</option> <option value="saab">Saab</option> <option value="fiat">Fiat</option> <option value="audi">Audi</option> </select> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
obsidian Posted May 4, 2007 Share Posted May 4, 2007 You could try something like this: function shortenSelectOptions(x) { eles = document.getElementByTagName('option'); for (i = 0; i < eles.length; i++) { if (eles[i].innerHTML.length > x) { newVal = eles[i].innerHTML.substring(0, x); eles[i].innerHTML = newVal; } } } That function could be called (passing in a value that is the length to which you wish to limit you options), and it should trim all the ones on the page to your specification. Hope that helps. Quote Link to comment Share on other sites More sharing options...
solarisuser Posted May 4, 2007 Author Share Posted May 4, 2007 Thanks.. And how would I call it? =) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.