When a user selects text from the dropdown menu, I want to remove any extra spaces. However, I am having trouble implementing this feature.
/* $("#input_13").find("option").each(function (index, option) {
$(option).html($(option).html().replace(/ /g,''));
}); */
$('#input_13').change(function() {
// Get the selected option
var selectedOption = $(this).find('option:selected');
// Get the current text, trim spaces, and set it back
var trimmedText = $.trim(selectedOption.text());
selectedOption.text(trimmedText);
// Optionally, update the value attribute if needed
// $(this).val(trimmedText);
console.log($("#input_13 option:selected").text())
});
<select id="input_13">
<option label=" sw" value="number:1902"> sw</option>
<option label=" s" value="number:1903"> s</option>
<option label=" dsdsdsds" value="number:1906"> dsdsdsds</option>
<option label=" swww" value="number:1905"> swww</option>
</select>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
The displayed value in the selection should show the trimmed text.
Note:- The value selected in the dropdown should only display the trimmed text when shown, while the options should remain as they are.