Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery: How do I select a value from a dropdownlist case insensitive?

My scenario:

  • I have a textbox that the user enters text
  • The textbox has an onblur function that tries to select a value from a dropdownlist (if it exists) based on the textbox input

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?

like image 874
Bryan Denny Avatar asked Aug 06 '09 14:08

Bryan Denny


People also ask

How can I get the selected value of a drop-down list with jQuery?

Use the jQuery: selected selector in combination with val () method to find the selected option value in a drop-down list.

How do I get the first select option in jQuery selected?

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).

How do you select a value in JavaScript?

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).


1 Answers

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.

like image 182
Patrick McElhaney Avatar answered Sep 19 '22 15:09

Patrick McElhaney