简体   繁体   中英

Html dropdown with fontawesome icons

What i have: An html dropdown like this:

<select name="faDropdown" class="form-control"> <option value="1">Select an icon</option> </select>

What i want: The dropdown to be filled with fontawesome icons. (not hardcoded like: <option value="fa fa-icon"></option> because I don't want to hardcode like 100 different icons in my dropdown).

I don't know if there is a solution for this so any help would be great :)

Do you want to use vanilla JS? You can try this example, but how it will work depends on how you want to use it. And anyway you will need to hardcode the icons in the array.

If you have questions describe please where you want to place this code.

 var select = document.getElementsByClassName('form-control')[0]; var icons = ['fa-icon', 'fa-icon2', 'fa-icon3']; for (var i = 0; i < icons.length; i++){ var opt = document.createElement('option'); opt.value = `fa ${icons[i]}`; opt.innerHTML = opt.value; select.appendChild(opt); } 
 <select name="faDropdown" class="form-control"> <option value="1">Select an icon</option> </select> 

I guess you could do that?

("&#xf0c1;" is just an example of a fontawesome icon).

<select name="faDropdown" class="form-control fa">
<option value="1">&#xf0c1;</option>
</select>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM