how set selection in select element when the name has â€[]†Hi folks, I wrote: <form name=" formname "> <select name=" dropdownboxname[]" class="inputbox" size="0" mosreq="0" moslabel="השכלה"> <option value=""> </option> <option value="x" id="228">תיכון</option> <option value="y" id="228">על-×ª×™×›×•× ×™</option> <option value="z" id="228">תעודה</option> </select> <input type="text" name="txt" size="20px" value="aaaaaaaa" /> </form> <script> ... for (var i=0; i < document.formname.dropdownboxname.length; i++) { if (document.formname.dropdownboxname[i].value == “valueâ€) { document.formname.dropdownboxname[i].selected = true; } } … Code (markup): It doesn’t set the selection because the name of the dropdown: If it was without the “[]†then there is no problem, but I must use the “[]†So how can I selection in a dropdownboxname[] Thanks.
Try using getElementsByTagName function. Example: <select id="dropdownboxname[]" onchange="f_selected(this.selectedIndex)" onfocus="f_selected(this.selectedIndex)" > <option value=""> </option> <option value="x" id="228">?????</option> <option value="y" id="228">??-??????</option> <option value="z" id="228">?????</option> </select> ... function f_selected(p_index) { var v = document.getElementsByTagName( "option" )[p_index]; ... } Code (markup): And just out of curiosity, why do you need '[]' on your name ? Surely you don't need that name .
The thing with using getElementsByTagName in this case is, you may have another select and group of options. You can either give the select tag an ID, then refer to it as: document.getElementById("theSelectTagsId"), or give the form an ID, then refer to the select tag as document.formsId.elements["yourSelectTagsName[]"]. As pointed above, you need to put "[]" in the name only if it's an array element (multiple values are expected). e.g.: - <input type="checkbox" ...> - <select multiple="multiple" ...>