We Recommend

HTML: The Definitive Guide HTML: The Definitive Guide
They teach you that learning HTML is like learning any other language and that reading a book of rules can only take you so far. Readers begin writing what may be their first Web page just two pages into the book's second chapter. From there on, they provide a wide range of HTML coding to allow readers to learn from good examples. The book includes a handy "cheat sheet" of HTML codes for quick reference.


Posted By

localhorst on 05/16/08


Tagged

fix select ie option disable htc


Versions (?)


disable select or option in IE


Published in: HTML 


URL: http://apptaro.seesaa.net/article/21140090.html

IE can't handle [ option disabled="disabled" ] tags, so this htc will help

  1. == HTML ================
  2.  
  3. <!--[if IE]>
  4. <style type="text/css">
  5. select, option { behavior: url("disable.htc"); }
  6. </style>
  7. <![endif]-->
  8. </head>
  9. <option>Option 1</option>
  10. <option>Option 2</option>
  11. <option disabled>Option 3</option>
  12. </select>
  13. </form>
  14. </body>
  15. </html>
  16.  
  17. == disable.htc ================
  18.  
  19. <?xml version="1.0" encoding="ISO-8859-1"?>
  20. <PUBLIC:COMPONENT LIGHTWEIGHT="true">
  21. <PUBLIC:ATTACH EVENT="ondocumentready" ONEVENT="onDocumentReady()" />
  22. <PUBLIC:ATTACH EVENT="ondetach" ONEVENT="onDetach()" />
  23.  
  24. <SCRIPT type="text/javascript">
  25. //<![CDATA[
  26.  
  27. var nLastSelectedIndex;
  28. var fOnChangeOriginal;
  29.  
  30. // event handlers
  31.  
  32. function onDocumentReady() {
  33. var sTag = element.tagName.toLowerCase();
  34. if (sTag == "select") {
  35. attachEvent("onchange", onChangeSelect);
  36. attachEvent("onpropertychange", onPropertyChangeSelect);
  37. nLastSelectedIndex = element.selectedIndex;
  38. hackOnChange();
  39. } else if (sTag == "option") {
  40. attachEvent("onpropertychange", onPropertyChangeOption);
  41. emulateOption();
  42. }
  43. }
  44.  
  45. function onDetach() {
  46. var sTag = element.tagName.toLowerCase();
  47. if (sTag == "select") {
  48. detachEvent("onchange", onChangeSelect);
  49. detachEvent("onpropertychange", onPropertyChangeSelect);
  50. } else if (sTag == "option") {
  51. detachEvent("onpropertychange", onPropertyChangeOption);
  52. }
  53. }
  54.  
  55. //
  56.  
  57. function onChangeSelect() {
  58. if (element.options[element.selectedIndex].disabled) {
  59. element.selectedIndex = nLastSelectedIndex;
  60. } else {
  61. nLastSelectedIndex = element.selectedIndex;
  62. if (fOnChangeOriginal != undefined) {
  63. fOnChangeOriginal();
  64. }
  65. }
  66. }
  67.  
  68. function onPropertyChangeSelect() {
  69. var sChangedPropertyName = event.propertyName.toLowerCase();
  70. if (sChangedPropertyName == "onchange") {
  71. hackOnChange();
  72. } else if (sChangedPropertyName == "selectedindex") { // contributed by Zecc
  73. nLastSelectedIndex = element.selectedIndex;
  74. }
  75. }
  76.  
  77. function onPropertyChangeOption() {
  78. var sChangedPropertyName = event.propertyName.toLowerCase();
  79. if (sChangedPropertyName == "disabled") {
  80. emulateOption();
  81. }
  82. }
  83.  
  84. // hack onChange attribute of select tag
  85.  
  86. function hackOnChange() {
  87. detachEvent("onpropertychange", onPropertyChangeSelect);
  88. fOnChangeOriginal = element.onchange;
  89. element.onchange = null;
  90. attachEvent("onpropertychange", onPropertyChangeSelect);
  91. }
  92.  
  93. // emulate disabled option
  94.  
  95. function emulateOption() {
  96. if (element.disabled) {
  97. element.style.color = "graytext";
  98. } else {
  99. element.style.color = "menutext";
  100. }
  101. }
  102.  
  103. //]]>
  104. </SCRIPT>
  105. </PUBLIC:COMPONENT>

Report this snippet 

You need to login to post a comment.