if you want add icon to select box. you can add select2 and add icon to your select box. in hear get icon from data-icon
set it to your selection box when select2 initializing
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Select-Box</title>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/select2.min.css" rel="stylesheet" />
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/select2.min.js"></script>
</head>
<body>
<select id="select-icon" name="vehicle" id="vehicle">
<option value="bicycle" data-icon="fa-bicycle">bicycle</option>
<option value="car" data-icon="fa-car">car</option>
<option value="van" data-icon="fa-shuttle-van">van</option>
</select>
<script>
function formatText (icon) {
return $('<span><i class="fas ' + $(icon.element).data('icon') + '"></i> ' + icon.text + '</span>');
};
$(document).ready(function() {
$('#select-icon').select2({
width:'50%',
templateSelection: formatText,
templateResult: formatText
});
});
</script>
</body>
</html>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…