javascript - Make the selected option uppercase in a dropdown using vanilla JS -
i need make text uppercase on selected option only in <select>
. found working example using jquery need convert vanilla js.
i've got pretty close, when choose option, makes selected value uppercase. when choose another, leaves previous option uppercase also.
i can't figure out how "capitalise options except selected option, uppercase".
any guidance appreciated.
how using css
* { font-family: arial; } select option { text-transform: capitalize; } select, select option:checked { text-transform: uppercase; }
<select name="title" id="title"> <option selected="" disabled="">categories</option> <option value="photo-galleries">photo galleries</option> <option value="photography">photography</option> <option value="romeo-juliet">romeo & juliet</option> <option value="swan-lake">swan lake</option> <option value="symmetries">symmetries</option> </select> <select name="day" id="day"> <option selected="" disabled="">day of week</option> <option value="monday">monday</option> <option value="tuesday">tuesday</option> <option value="wednesday">wednesday</option> <option value="thursday">thursday</option> <option value="friday">friday</option> <option value="saturday">saturday</option> <option value="sunday">sunday</option> </select>
here's javascript version well
[].slice.call( document.queryselectorall('option') ).foreach(ucfirst); document.getelementbyid('title').addeventlistener('change', fn, false); document.getelementbyid('day').addeventlistener('change', fn, false); function ucfirst(el) { el.innerhtml = el.innerhtml.charat(0).touppercase() + el.innerhtml.slice(1).tolowercase(); } function fn() { var options = this.getelementsbytagname('option'); var selected = options[this.selectedindex]; [].slice.call(options).foreach(ucfirst); selected.value = selected.innerhtml = selected.innerhtml.touppercase(); }
Comments
Post a Comment