Link to home
Start Free TrialLog in
Avatar of REA_ANDREW
REA_ANDREWFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Expected Identifier

Hi, I have a script which re-orders and moves options from one select box to another.

the first script is moveToList, which I got from quirksmode

the second which re-orders the options, I got from Experts-Exchange which mplunjan wrote.

Separately they work, but putting them together cause an error. Here is the whole html script, if you paste it in you will see what I mean and hopefully someone can give me an idea of how to solve this.

Thanks in advance.

Andrew

<html>
<head>
     <title>Reorder script</title>
<script>

function copyToList(from,to)
{
  fromList = eval('document.forms[0].' + from);
  toList = eval('document.forms[0].' + to);
  if (toList.options.length > 0 && toList.options[0].value == 'temp')
  {
    toList.options.length = 0;
  }
  var sel = false;
  for (i=0;i<fromList.options.length;i++)
  {
    var current = fromList.options[i];
    if (current.selected)
    {
      sel = true;
      if (current.value == 'temp')
      {
        alert ('You cannot move this text!');
        return;
      }
      txt = current.text;
      val = current.value;
      toList.options[toList.length] = new Option(txt,val);
      fromList.options[i] = null;
      i--;
    }
  }
  if (!sel) alert ('You haven\'t selected any options!');
}

/* Reorder script<BR>   Copyright (c) 2002 Michel Plungjan "michel at irt.org"
*/

function opt(txt,val,sel) {
   this.txt=txt;
   this.val=val;
   this.sel=sel;
   
}
function move(list,to) {    
    var total=list.options.length;
    index = list.selectedIndex;
    if (index == -1) return false;
    if (to == +1 && index == total-1) return false;
    if (to == -1 && index == 0) return false;
    to = index+to;
    var opts = new Array();
    for (i=0; i<total; i++) {
      opts[i]=new opt(list.options[i].text,list.options[i].value,list.options[i].selected);
    }
    tempOpt = opts[to];
    opts[to] = opts[index];
    opts[index] = tempOpt
    list.options.length=0; // clear

    for (i=0;i<opts.length;i++) {
       list.options[i] = new Option(opts[i].txt,opts[i].val);
       list.options[i].selected = opts[i].sel;
    }
    list.focus();
}
</script>  
</head>

<body>

<form name="form1" action="...">
<select MULTIPLE name="sel">
<option value="zero">0</option>
<option value="one">1</option>
<option value="two">2</option>
<option value="three">3</option>
</select>
<input type="button" onClick="move(this.form.sel,-1);" value="/\">
<input type="button" onClick="move(this.form.sel,+1);" value="\/">
<input type="button" onClick="copyToList(sel,sel2);" value=">">
<input type="button" onClick="copyToList(sel2,sel);" value="<">
<select MULTIPLE name="sel2">

</select>
<input type="submit">
</form>


</body>
</html>
Avatar of Roonaan
Roonaan
Flag of Netherlands image

Try:

<input type="button" onClick="copyToList(this.form.sel,this.form.sel2);" value=">">
<input type="button" onClick="copyToList(this.form.sel2,this.form.sel);" value="<">

-r-
Avatar of REA_ANDREW

ASKER

Roonaan,

I still get the same error. At the top of the script It uses the Eval function, to resolve the form object.

  fromList = eval('document.forms[0].' + from);
  toList = eval('document.forms[0].' + to);
I have checked and I cannot find any common calls that may be messing it up. Strange.
Yes, sorry, I had to mention you could change your function to also:

function copyToList(fromList,toList)

-r-
ASKER CERTIFIED SOLUTION
Avatar of smaccari
smaccari

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of smaccari
smaccari

(placing quotes on sel and sel2 in the function call, that should be enough for your code to work)
smaccari

cheers, missed that one lol.

this works now 75%, although if you paste it in and try it you will see that it does not allow the transfer from right to left.

This functionality is available when the script is on its own, but it is not working in this page when I conbine it with the re-ordering script.
You should quote it

<input type="button" onClick="copyToList('sel','sel2');" value="&gt;">
<input type="button" onClick="copyToList('sel2','sel');" value="&lt;">
KennyTM

Cheers, in my previous post I have explained that, that is fixed now but still I cannot transfer options from Right To Left.  Although putting the script on its own, enables this functionality.

if you paste the code and run it you will see what I mean,

Thanks very much for your time by the way

Andrew
(well I think I was a bit slow; anyway the script works for me, in IE, Fx & Opera)
The transfer works for me in both directions..
thanks everyone.  

smaccari
 hit the nail on the head first, and I think you all agree that derserves the points.

I will wait for a bi, if anyone does not agree with me there, other wise I will Accept

smaccari

Answer

BUT thankyou all very much for your time on this problem also.

Cheers guys

Andrew (Will accept it in 15 mins)