code_junkie

Check if option is selected with jQuery, if not select a default

Nov 11th, 2011
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. <select id="mySelect" multiple="multiple">
  2. <option value="1">First</option>
  3. <option value="2">Second</option>
  4. <option value="3">Third</option>
  5. <option value="4">Fourth</option>
  6. </select>
  7.  
  8. <script type="text/javascript">
  9. $(document).ready(function() {
  10. if (!$("#mySelect option:selected").length)
  11. $("#mySelect option[value='3']").attr('selected', 'selected');
  12. });
  13. </script>
  14.  
  15. var foo = document.getElementById('yourSelect');
  16. if (foo)
  17. {
  18. if (foo.selectedIndex != null)
  19. {
  20. foo.selectedIndex = 0;
  21. }
  22. }
  23.  
  24. function selectOption(select_id, option_val) {
  25. $('#'+select_id+' option:selected').removeAttr('selected');
  26. $('#'+select_id+' option[value='+option_val+']').attr('selected','selected');
  27. }
  28.  
  29. <script type="text/javascript">
  30. $(document).ready(function() {
  31. if (!$("#mySelect option:selected").length)
  32. $("#mySelect").val( 3 );
  33.  
  34. });
  35. </script>
  36.  
  37. $("#mySelect").val( 3 );
  38.  
  39. $(".Result").html($("option:selected").text());
  40.  
  41. $(document).ready(function(){
  42. if ( $("#context").selectedValues() == false) {
  43. $("#context").selectOptions("71");
  44. }
  45. });
  46.  
  47. $("option[value*='2']").attr('selected', 'selected');
  48. // 2 for example, add * for every option
Add Comment
Please, Sign In to add comment