Arranging/swapping drop down box values

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shrutib
    New Member
    • Mar 2013
    • 4

    Arranging/swapping drop down box values

    Hello All,
    I am having the drop down list with count 1 to 10 which is looped for itemlist. For item 1 dropdown list shows count 1 to 10,item 2 also dropdown list shows count 1 to 10 and so on. I want to preset the value for the drop down list which should display on page load like for item 1 drop down value count set to 1, for item 2 drop down value count set 2.. and so on. How can i acheive this please let me know. Code is attached for the drop down list. Want the solution in html/javascript/ruby on rails.
    Code:
        <select id="order_of_display" name="order[display]">
                                                        <option value="0" selected ="selected">Selectorder</option>
     <% if  @items[:active] %>
                                                        <%for i in (1..@items[:active].size) %>
                                                             <option value=" <%= i  %>" > <%= item.id %> </option>
                                                             <% end %>
                                                              <% end %>
                                                      </select>
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    You would probably be best setting this in Ruby/Rails.

    You've already set the selected option value:
    Code:
    selected ="selected"
    Add this to the desired option value setting for each drop down.

    If you still need this done in JavaScript, let us know.

    Comment

    • shrutib
      New Member
      • Mar 2013
      • 4

      #3
      Hello,
      I am able to view the selected one. Now my problem is i want to swap the drop down values on change. For example
      drop down 1 - value selected 1
      drop down 2 - value selected 2 and so on. If i change drop down 1 to value 2 , drop down 2 value should change to 1 automatically(s wapping). What should i do to achieve this?Please help.
      Code:
      [LEFT]<select id="order_of_display" name="order[display]" onchange="swap();">
      
                                                  <% @orderNumber= item.position  %>
                                                  <%for i in (1..@items[:active].size) %>
      
                                                  <%if( i == @orderNumber) %>
      
                                                  <option value="<%= item.id %> " selected="selected"><%= i%>  </option>
      
                                                  <%else%>
                                                  <option value="<%= item.id %>" ><%= i%> </option>
                   <%end%>
      
                                                  <%end%>
                                                  </select>[/LEFT]

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        The idea is simple, e.g.
        Code:
        var val1 = dropdown1.value;
        dropdown1.value = dropdown2.value;
        dropdown2.value = val1;

        Comment

        Working...