code_junkie

Add options to select box without Internet Explorer closing the box?

Nov 11th, 2011
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4. function appendOption(select,option) {
  5. try {
  6. selectBox.add(option,null); // Standards compliant.
  7. } catch (e) {
  8. selectBox.add(option); // IE only version.
  9. }
  10. }
  11.  
  12. function loadOptions() {
  13. // Simulate an AJAX request that will call the
  14. // loadOptionsCallback function after 500ms.
  15. setTimeout(loadOptionsCallback,500);
  16. }
  17.  
  18. function loadOptionsCallback() {
  19. var selectBox = document.getElementById('selectBox');
  20. var option = document.createElement('option');
  21. option.text = 'new option';
  22. appendOption(selectBox,option);
  23. }
  24. </script>
  25. </head>
  26. <body>
  27. <select id="selectBox" onmousedown="loadOptions();">
  28. <option>Any</option>
  29. </select>
  30. </body>
  31. </html>
  32.  
  33. $(function() {
  34. jQuery(".selectBox").live("focus", function() {
  35. loadOptions();
  36. });
  37. });
  38.  
  39. <html>
  40. <head>
  41. <script src="jquery-latest.js" type="text/javascript"/>
  42. </head>
  43. <body>
  44. <select id="selectBox" onmousedown="loadOptions();">
  45. <option>Any</option>
  46. </select>
  47. <script type="text/javascript">
  48. $(function() {
  49. jQuery(".selectBox").live("focus", function() {
  50. loadOptions();
  51. });
  52. });
  53. function appendOption(select, option) {
  54. try {
  55. selectBox.add(option, null); // Standards compliant.
  56. } catch (e) {
  57. selectBox.add(option); // IE only version.
  58. }
  59. }
  60.  
  61. function loadOptions() {
  62. // Simulate an AJAX request that will call the
  63. // loadOptionsCallback function after 500ms.
  64. setTimeout(loadOptionsCallback, 500);
  65. }
  66.  
  67. function loadOptionsCallback() {
  68. var selectBox = document.getElementById('selectBox');
  69. var option = document.createElement('option');
  70. option.text = 'new option';
  71. appendOption(selectBox, option);
  72. }
  73. </script>
  74. </body>
Add Comment
Please, Sign In to add comment