I have a select element which will be displayed on mobile device
<form>
<select id="selectMe" >
<optgroup label="Australia">
<option value="AustCars">Australia Cars Report</option>
<option value="AustBikes">Australia Bikes Report</option>
</optgroup>
<optgroup label="New Zealand">
<option value="NZCars">New Zealand Cars Report</option>
<option value="NZBikes">New Zealand Bikes Report</option>
</optgroup>
</select>
</form>
Two things to note:
- I want to display the full name
Australia Cars Reportwhen closed, to remove the ambiguity between Australia and New Zealand. - I want to use the shorter version
Cars Reportwhen theselectis opened, because the full nameAustralia Cars Reportis too long for the iPhone selector (not actual names, but renders asAustr...rs Reportor similar) and the country is implicit in theoptgroup.
My approach (based on Change a select list display value upon selecting description) is to modify the value of each item when the control gets focus, and then un-modify when it loses focus. And that’s about where I get stuck.
I have two problems here:
- I can get the focus event, but there’s not ‘lostfocus’ event. I can’t use the ‘changed’ event because the user might not have actually changed the selection.
- I don’t know how to get the value of the
optgroupto add and remove from the option text. By this I mean that I need to find, forAustralia Cars Report, that it is nested underAustraliaand thus I can remove that word from the display text.
I can use javascript or jQuery, or other tools if applicable. Open to suggestions of an easy way to do this that I’ve overlooked, too.
Use
blur.A little unclear, but I’ll hazard a few guesses to get you started:
You’ll need to give the specific group an ID to access it uniquely, e.g.
var fooElement = document.getElementById("foo")fooElement.selectedfooElement.options[]. Adding and removing new DOM elements to this list will add or remove them in the HTML DOM.optioncan be found using.valueor.text.Try looking up the
selecttag in HTML, and the javascript to interact with that type of element. Good luck.