I'm trying to make a default selection to clear a dropdown selection.
If I select the dropdown with HTML, it works, however, when I generate the same dropdown with Javascript, it does not work.
Please
& lt; Id = "myDropdown" & gt; & Lt; Options & gt; Option 1 & lt; / Options & gt; & Lt; Options & gt; Option 2 & lt; / Options & gt; & Lt; Options & gt; Option 3 & lt; / Options & gt; & Lt; / Select & gt; & Lt; Div id = "yo" & gt; & Lt; / Div & gt; Document.getElementById ("myDropdown"). SelectedIndex = 1 var for my_select = document.createElement ("I select") (var i = 0; i & lt; 3; i ++) {my_select.options [i] = new option (i, i)} my_select .selectedIndex = -1 document.getElementById ("yo"). AppendChild (my_select) Note that the first dropdown is empty, and the second is not.
Apparently you select a of selectedIndex element that has not yet been entered in the DOM If you move my_select.selectedIndex = 1 after the attachment So it works fine. var my_select = Document.createElement ("select"); For (var i = 0; i .
No comments:
Post a Comment