how set selection in select element when the name has ”[]”

Discussion in 'JavaScript' started by Betty_S, Mar 6, 2007.

  1. #1
    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[]:confused:

    Thanks.
     
    Betty_S, Mar 6, 2007 IP
  2. ajsa52

    ajsa52 Well-Known Member

    Messages:
    3,426
    Likes Received:
    125
    Best Answers:
    0
    Trophy Points:
    160
    #2
    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
    .
     
    ajsa52, Mar 6, 2007 IP
  3. phper

    phper Active Member

    Messages:
    247
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #3
    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" ...>
     
    phper, Mar 6, 2007 IP