Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 21st, 2012  |  syntax: None  |  size: 0.74 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Get selected key/value of a combo box using jQuery
  2. $(this).find("select").each(function () {
  3.     if ($.trim($(this).val()) != '') {
  4.         searchString += $.trim($(this).val()) + " "; //This gives me the key. How can I get the value also?
  5.     }
  6. });
  7.        
  8. <select>
  9.     <option value="KEY">VALUE</option>
  10. </select>
  11.        
  12. $(this).find('option:selected').text();
  13.        
  14. $(this).find("select").each(function () {
  15.     $(this).find('option:selected').text();
  16. });
  17.        
  18. <select name="foo" id="foo">
  19. <option value="1">a</option>
  20. <option value="2">b</option>
  21. <option value="3">c</option>
  22. </select>
  23. <input type="button" id="button" value="Button" />
  24.  
  25. $('#button').click(function() {
  26.     alert($('#foo option:selected').text());
  27.     alert($('#foo option:selected').val());
  28. });