selected-index.html (3724B)
1 <!doctype html> 2 <meta charset=utf-8> 3 <title>HTMLSelectElement selectedIndex</title> 4 <script src="/resources/testharness.js"></script> 5 <script src="/resources/testharnessreport.js"></script> 6 <div id=log></div> 7 8 <form id=form> 9 <select id=empty></select> 10 11 <select id=default> 12 <option></option> 13 <option></option> 14 <option></option> 15 <option></option> 16 <option></option> 17 </select> 18 19 <select id=disabled> 20 <option disabled></option> 21 <option></option> 22 </select> 23 24 <select id=selected> 25 <option></option> 26 <option selected></option> 27 </select> 28 29 <select id=display-none> 30 <option style="display:none"></option> 31 <option></option> 32 </select> 33 34 <select id=minus-one> 35 <option value=1>1</option> 36 <option value=2>2</option> 37 </select> 38 </form> 39 40 <script> 41 function assertSelectedIndex(select, value) { 42 assert_equals(select.selectedIndex, value); 43 assert_equals(select.options.selectedIndex, value); 44 } 45 46 function assertSelectValue(select, value) { 47 assert_equals(select.value, value); 48 } 49 50 test(function () { 51 var select = document.getElementById('empty'); 52 assertSelectedIndex(select, -1); 53 }, "get empty"); 54 55 test(function () { 56 var select = document.getElementById('default'); 57 assertSelectedIndex(select, 0); 58 }, "get default"); 59 60 test(function () { 61 var select = document.getElementById('disabled'); 62 assertSelectedIndex(select, 1); 63 }, "get disabled"); 64 65 test(function () { 66 var select = document.getElementById('selected'); 67 assertSelectedIndex(select, 1); 68 }, "get unselected"); 69 70 test(function () { 71 var select = document.getElementById('empty'); 72 select.selectedIndex = 1; 73 assertSelectedIndex(select, -1); 74 }, "set empty (HTMLSelectElement)"); 75 76 test(function () { 77 var select = document.getElementById('empty'); 78 select.options.selectedIndex = 1; 79 assertSelectedIndex(select, -1); 80 }, "set empty (HTMLOptionsCollection)"); 81 82 test(function () { 83 var select = document.getElementById('default'); 84 assertSelectedIndex(select, 0); 85 select.selectedIndex = 2; 86 assertSelectedIndex(select, 2); 87 this.add_cleanup(() => { select.selectedIndex = 0; }); 88 }, "set (HTMLSelectElement)"); 89 90 test(function () { 91 var select = document.getElementById('default'); 92 assertSelectedIndex(select, 0); 93 select.options.selectedIndex = 2; 94 assertSelectedIndex(select, 2); 95 this.add_cleanup(() => { select.selectedIndex = 0; }); 96 }, "set (HTMLOptionsCollection)"); 97 98 test(function () { 99 var select = document.getElementById('selected'); 100 var form = document.getElementById('form'); 101 assertSelectedIndex(select, 1); 102 select.selectedIndex = 0; 103 assertSelectedIndex(select, 0); 104 form.reset(); 105 assertSelectedIndex(select, 1); 106 }, "set and reset (HTMLSelectElement)"); 107 108 test(function () { 109 var select = document.getElementById('selected'); 110 var form = document.getElementById('form'); 111 assertSelectedIndex(select, 1); 112 select.options.selectedIndex = 0; 113 assertSelectedIndex(select, 0); 114 form.reset(); 115 assertSelectedIndex(select, 1); 116 }, "set and reset (HTMLOptionsCollection)"); 117 118 test(function () { 119 var select = document.getElementById('display-none'); 120 assertSelectedIndex(select, 0); 121 }, "get display:none"); 122 123 test(function () { 124 var select = document.getElementById('display-none'); 125 select.offsetTop; // force rendering 126 assertSelectedIndex(select, 0); 127 select.options[1].selected = true; 128 assertSelectedIndex(select, 1); 129 select.options[1].selected = false; 130 assertSelectedIndex(select, 0); 131 }, "reset to display:none"); 132 133 test(function() { 134 var select = document.getElementById("minus-one"); 135 assertSelectedIndex(select, 0); 136 137 select.selectedIndex = -1; 138 139 assertSelectedIndex(select, -1); 140 assertSelectValue(select, ""); 141 142 }, "set selectedIndex=-1"); 143 </script>