faqts : Computers : Programming : Languages : JavaScript : Forms : SELECT

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

25 of 64 people (39%) answered Yes
Recently 5 of 10 people (50%) answered Yes

Entry

How can I move the selected OPTION up or down?
It is not working in Firefox and in IE too... :( Object not support automation or object is read onl

Oct 3rd, 2000 16:31
Martin Honnen, Milan Kajnar,


The following contains code for that:
<HTML>
<HEAD>
<SCRIPT>
function moveOptionUp (select) {
  if (select.selectedIndex > 0) {
    var s = select.selectedIndex;
    var option = new Object();
    for (var property in select.options[s])
      option[property] = select.options[s][property];
    for (var property in select.options[s - 1])
      select.options[s][property] = select.options[s - 1][property];
    for (var property in option)
      select.options[s - 1][property] = option[property];
  }
}
function moveOptionDown (select) {
  if (select.selectedIndex < select.options.length - 1) {
    var s = select.selectedIndex;
    var option = new Object();
    for (var property in select.options[s])
      option[property] = select.options[s][property];
    for (var property in select.options[s + 1])
      select.options[s][property] = select.options[s + 1][property];
    for (var property in option)
      select.options[s + 1][property] = option[property];
  }
}
</SCRIPT>
</HEAD>
<BODY>
<FORM NAME="formName">
<SELECT NAME="aSelect">
<SCRIPT>
for (var i = 1; i <= 20; i++)
  document.write('<OPTION>' + i)
</SCRIPT>
</SELECT>
<INPUT TYPE="button" VALUE="up"
       ONCLICK="moveOptionUp (this.form.aSelect)"
>
<INPUT TYPE="button" VALUE="down"
       ONCLICK="moveOptionDown (this.form.aSelect)"
>
<BR>
<SELECT NAME="a2ndSelect" SIZE="20">
<SCRIPT>
for (var i = 1; i <= 20; i++)
  document.write('<OPTION>' + i)
</SCRIPT>
</SELECT>
<INPUT TYPE="button" VALUE="up"
       ONCLICK="moveOptionUp (this.form.a2ndSelect)"
>
<INPUT TYPE="button" VALUE="down"
       ONCLICK="moveOptionDown (this.form.a2ndSelect)"
>
</FORM>
</BODY>
</HTML>