By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,259 Members | 1,714 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,259 IT Pros & Developers. It's quick & easy.

Swap values of Drop Down List

P: 4
Hello All,
I have list of DropDownList of items fetching from database, all have values of 1,2 and 3...
DropDownList1 has a selected value of 1
DropDownList2 has a selected value of 3
DropDownList3 has a selected value of 2
What I want is to swap values. It's urgent please help.
i.e. if I selected number 2 in DropDownList1, DropDownList3 should select 1 automatically.
Using Ruby on Rails to fetch the data from db, Ajax,Javascript & HTML. I tried this code from this website http://www.webdeveloper.com/forum/showthread.php?231117-Swap-values-of-DropDownList , no luck.I am looking for this exact functionality. Please help if i can get solution in js & ajax.
Below is the ruby code for fetching the details.
Expand|Select|Wrap|Line Numbers
  1. <select id="order_of_display" name="order[display]" >
  2. <% @orderNumber= item.position %>
  3. <%for i in (1..@items[:active].size) %>
  4. <%if( i == @orderNumber) %>
  5. <option value="<%= item.id %> " selected="selected"><%= i%></option>
  6. <%else%>
  7. <option value="<%= item.id %>"><%= i%></option>
  8. <%end%>
  9. <%end%>
  10. </select>
  11.  
Apr 8 '13 #1
Share this Question
Share on Google+
3 Replies


10K+
P: 13,264
Post the javascript code that you tried and explain where you are stuck with it.
Apr 8 '13 #2

P: 4
Below is the ruby code and javascript. On mouse up, i am getting the existing selected text value. On change, the other selected text value. I want the existing text value inside changeOrder function. Where am i wrong?Please correct me.

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. var currentVal;
  3. function getCurrentVal(selectItem)
  4. {
  5. currentVal=selectItem.options[selectItem.selectedIndex].text;
  6. return currentVal;
  7. }
  8. function changeOrder(sel)
  9. {
  10. alert(getCurrentVal());
  11. posval = sel.options[sel.selectedIndex].text;
  12.  
  13. }
  14. </script>
  15. <select id="order_of_display" name="order[display]" onmouseup="getCurrentVal(this);"  onchange="changeOrder(this);">
  16. <% @orderNumber= item.position %>
  17. <%for i in (1..@items[:active].size) %>
  18. <%if( i == @orderNumber) %>
  19. <option value="<%= item.id %> " selected="selected"><%= i%></option>
  20. <%else%>
  21. <option value="<%= item.id %>"><%= i%></option>
  22. <%end%>
  23. <%end%>
  24. </select>
  25.  
Apr 8 '13 #3

10K+
P: 13,264
Do a test without the ruby, just hard coding some select option values and get it to work like that. If it does work and the ruby is the problem then ask in a ruby forum.
Apr 9 '13 #4

Post your reply

Sign in to post your reply or Sign up for a free account.