My scenario:
This works perfectly fine if the textbox value is the same case as the dropdownlist value. But I want it to be case insensitive.
So if the user types "stackoverflow" then I want it to select from the dropdownlist "StackOverflow". How can I do this via jQuery?
Use the jQuery: selected selector in combination with val () method to find the selected option value in a drop-down list.
Select the <select> element using JQuery selector. This selector is more specific and selecting the first element using option:nth-child(1). This will get access to the first element (Index starts with 1).
To get the value of a select or dropdown in HTML using pure JavaScript, first we get the select tag, in this case by id, and then we get the selected value through the selectedIndex property. The value "en" will be printed on the console (Ctrl + Shift + J to open the console).
Here's another approach: find the matching value in its actual case, "StackOverflow," and call val()
with that.
var matchingValue = $('#select option').filter(function () {
return this.value.toLowerCase() === 'stackoverflow';
} ).attr('value');
$('#select').val(matchingValue);
Of course, the literal 'stackoverflow'
should be replaced with a variable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With