473,770 Members | 2,160 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

changing order of elements in select menu

4 New Member
Hi,

I have constructed a clickable list of words which are added to a select box. I then have buttons to move the added elements up or down in the list.

The code below seems to work fine for the first change (UP only) but after that the last element in the list disappears :(

I have no idea why this is happening so could anyone offer a solution or a better method of doing the above task? Thanks in advance


Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4.  
  5. function moveText(name, id)
  6. {
  7.     var selected_box = document.getElementById("textarea_two");
  8.     var new_option = new Option(name, id);
  9.     selected_box.options[selected_box.length] = new_option;    
  10. }
  11.  
  12.  
  13. function moveup(){
  14.  
  15.     var y=document.getElementById("textarea_two").selectedIndex
  16.     var x=document.getElementById("textarea_two").options
  17.  
  18.     if (y>0){
  19.         var selected = x[y];
  20.         var dest = x[y-1];
  21.  
  22.         //swap option elements
  23.         x[y-1] = selected;
  24.         x[y] = dest;    
  25.  
  26.         //reset variables if necessary ?
  27.         x = null;
  28.         y = null;
  29.         selected = null;
  30.         dest = null;    
  31.     }
  32. }
  33. </script>
  34. </head>
  35.  
  36. <body>
  37. <table>
  38. <tr>
  39. <td id="textarea_one">
  40. <li id= "1" title="text one" ondblclick="moveText(this.title, this.id)">text one</li>
  41. <li id= "2" title="text two" ondblclick="moveText(this.title, this.id)">text two</li>
  42. <li id= "3" title="text three" ondblclick="moveText(this.title, this.id)">text three</li>
  43. </td>
  44. </tr>
  45. </table>
  46.  
  47. <br>
  48. <select id="textarea_two" style="width:150px;" size="7"></select>
  49. <br>
  50.  
  51. &nbsp;Move selected: <br>
  52. ><span onclick="moveup()">UP</span><br>
  53. ><span onclick="movedown()">DOWN</span>
  54.  
  55.  
  56. </body>
  57. </html>
Apr 25 '07 #1
1 3039
technod
4 New Member
I've made a work around for this problem. I used a string to store both the previous and next values of each option element that is being changed so as to avoid loosing the data, which is what was happening in the above code.

Hope this might be useful to somebody. If anyone knows a simpler way of doing this please let me know! Cheers

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4.  
  5. function moveText(name, id)
  6. {
  7.     var new_option = new Option(name, id);
  8.     var x=document.getElementById("textarea_two").options;
  9.     var y=document.getElementById("textarea_two").selectedIndex;
  10.     new_option.tabIndex = x.length;
  11.     //alert(new_option.tabIndex)
  12.     var counter = 0
  13.     //alert(new_option.text);
  14.     //alert(x.length);
  15.  
  16.  
  17.     //loop through option values if they exist and if the item has not already been added then add to list
  18.     if (x.length > 0){
  19.         for(var i=0;i<x.length;i++){    
  20.         //alert(x[i].text)
  21.             if (x[i].text == new_option.text){
  22.                 counter++;
  23.             }
  24.         }
  25.         //alert(counter);
  26.         if (counter == 0){
  27.             x[x.length] = new_option;
  28.         }else{
  29.             alert("item already added to calendar!");
  30.         }
  31.     }else{
  32.         x[x.length] = new_option;
  33.     }
  34.  
  35. }
  36.  
  37.  
  38. function reOrder(direction){
  39.  
  40.     //alert(direction);
  41.     var y=document.getElementById("textarea_two").selectedIndex;
  42.     var x=document.getElementById("textarea_two").options;
  43.  
  44.     var selected = x[y];
  45.  
  46.     //set the direction up or down
  47.     if (direction == "up" ){
  48.         var dest = x[y-1];
  49.     }else if (direction == "down"){
  50.         var dest = x[y+1];
  51.     }
  52.  
  53.     var textString = dest.text;
  54.     var valueString = dest.value;
  55.     //alert(textString)
  56.  
  57.     //make string containing both values so that the 'dest' data is not lost in swap
  58.     textString += "&"+selected.text;
  59.     valueString += "&"+selected.value;
  60.     //alert(textString)
  61.  
  62.     //first half equals destination text
  63.     var firstHalfTextString = textString.substr(0,[textString.indexOf("&")]) 
  64.     var firstHalfValueString = valueString.substr(0,[textString.indexOf("&")])
  65.     //alert(firstHalfTextString)
  66.  
  67.     //second half equals selected text
  68.     var secondHalfTextString = textString.substr(textString.indexOf("&")+1)
  69.     var secondHalfValueString = valueString.substr(textString.indexOf("&")+1)
  70.     //alert(secondHalfTextString)
  71.  
  72.     //swap the option elements text values
  73.     selected.text = firstHalfTextString;
  74.     dest.text = secondHalfTextString;
  75.     selected.value = firstHalfValueString;
  76.     dest.value = secondHalfValueString;
  77.  
  78.     //set the select to the moved element
  79.     if (direction == "up" ){
  80.         document.getElementById("textarea_two")[y-1].selected = true;
  81.     }else if (direction == "down"){
  82.         document.getElementById("textarea_two")[y+1].selected = true;    
  83.     }
  84.     //alert("tabindex="x.selected.tabIndex+"&value"+x.selected.value)****
  85.  
  86. }
  87.  
  88. </script>
  89. </head>
  90.  
  91. <body>
  92. <ul id="textarea_one">
  93. <li id= "1" title="text one" ondblclick="moveText(this.title, this.id)">text one</li>
  94. <li id= "2" title="text two" ondblclick="moveText(this.title, this.id)">text two</li>
  95. <li id= "3" title="text three" ondblclick="moveText(this.title, this.id)">text three</li>
  96. </ul>
  97.  
  98. <br>
  99. <select id="textarea_two" style="width:150px;" size="7"></select>
  100. <br>
  101.  
  102. &nbsp;Move selected: <br>
  103. ><span onclick="reOrder('up')">UP</span><br>
  104. ><span onclick="reOrder('down')">DOWN</span>
  105.  
  106.  
  107. </body>
  108. </html>
Apr 26 '07 #2

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

Similar topics

7
2294
by: Hal Vaughan | last post by:
I have a sample script from a book ("Beginning JavaScript" by Paul Wilton) that removes or adds a choice to a <SELECT> element. The <FORM> is form1 and the <SELECT> is theDay. The example uses these lines (full text is below): if (document.form1.theDay.options.text != "Wednesday) { var days = document.form1.theDay; days.options.text = days.options.text; <snip> var option - new Option("Wednesday", 2);
9
1931
by: F. Da Costa | last post by:
Hi, Does anybody know why IE5+ does *not* honour array objects (like a table) across a session? Example: Frame A contains a var tableVar which is set via form Frame B (on init) using top.A.tableVar = document.getElementById("someTable"); As long as Frame B is *not* 'refreshed/ reloaded' witk another page the
2
2510
by: Paolo Pignatelli | last post by:
I am trying to get an output/file like this (below) in an XML file (MyXmlFile.xml) (which I will use for a slide show) -- <gallery timer="3" order="sequential" fadetime="2" looping="yes" xpos="0" ypos="0"> <image path="images2/Test1.jpg" />
4
7563
by: dtwalter | last post by:
Is it possible to ORDER BY a SubSelect? I don't see why it wouldn't be, but I'm having some trouble. Hopefully it's just a simple error in syntax and somebody can tell me. Here's what I'm trying to do... I've got two tables: Table1: TestData Filename Bird FileB Blue FileA Circle FileC
1
1834
by: shankwheat | last post by:
I've been using this little script to change the <a href=""></a> value of a link which works well. <script language="javascript" type="text/javascript"> function ordering(sorder) { var link = document.getElementById("mylink").href = "http://www.mysite.com?Option="+sorder; } </script>
2
1389
by: Jason | last post by:
Hi all, I know how to do this the hard way, but I suspect that there's an easier option. I'm creating a program that requires a series of 4 or 6 select menus. Something like this: <select name="1"> <option value="a">A</option>
3
1557
by: groups2 | last post by:
I have a form with a dropdown (select) menu a text input field and some hidden values, along with an input botton that triggers an ajax function that submits the form. If the button is after the select statement, the chosen value of the selection of the dropdown is posted. But if the button is before the select statement, everything in the form is posted but the value of the selection Why would the select box be left out of the post ?
2
3206
by: paul | last post by:
I have a JS function to change the width of a <divthat works great in Firefox, but not at all in IE7. In IE an error message occurs: Line: 92 Char: 5 Error: Invalid Argument Code: 0 Firefox reports no errors in the Error Console, or Firebug, and the <divis resized correctly. Here is the function:
0
9618
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10260
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10101
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8933
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.