Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Moving data down a list

Status
Not open for further replies.

mych

Programmer
May 20, 2004
248
GB
Hi,

I have 6 groups of inputs (each consisting of one textarea, one textbox and one dropdown). In the first group the elements have a name and id of TA1, TT1, TD1, the second TA2, TT2, TT3 all the way to TA6, TT6, TD6.

Within Groups 2-5 I also have a 'Move Down' image with an onclick event that triggers the function MoveDown(Grp) where Grp would be the Group number 2 to 5

my original code of...
Code:
<script type="text/javascript">

function MoveDown(Grp)
{
  NGrp = Grp+1
  document.getElementById('TA'+NGrp ).value = document.getElementById('TA'+Grp).value
  document.getElementById('TB'+NGrp ).value = document.getElementById('TB'+Grp).value
 document.getElementById('TD'+NGrp ).selectedIndex = document.getElementById('TD'+Grp).selectedIndex
  document.getElementById('TA'+Grp).value = ""
  document.getElementById('TB'+Grp).value = ""
  document.getElementById('TD'+Grp).selectedIndex = 0
}

</script>

...would work ok. For example, if I clicked on the 'Move Down' image in group 2 the code would move the contents of TA2, TB2 to TA3 and TB3, set the dropdown value of TD3 to that of TD2 and finally making TA2 and TB2 blank and set TD to the first value of the dropdown (which happens to be 'Please Select'). All fine so far...

But I have 6 groups and what if say groups 1-4 were populated and then I wanted to move groups 2, 3 and 4 down one so that I could put in new values in group 2

The code I have come up with is....

Code:
<script type="text/javascript">

function MoveDown(Grp)
{
  for (i = 5; i = Grp; i--)
	{
	  j= i+1
	  document.getElementById('TA'+j).value = document.getElementById('TA'+i).value
  	  document.getElementById('TB'+j).value = document.getElementById('TB'+i).value
      document.getElementById('TD'+j).selectedIndex = document.getElementById('TD'+i).selectedIndex
	}
  document.getElementById('TA'+Grp).value = ""
  document.getElementById('TB'+Grp).value = ""
  document.getElementById('TD'+Grp).selectedIndex = 0
}

</script>

What I was hoping this would do is in the example I gave above... clicking on the Move Down image in group 2 should move the contents of each group down one starting with group 5 moving to 6 then 4 to 5, 3 to 4, 2 to 3 and then clear group 2.

I get no errors but IE hangs when the function starts. Have I created an endless loop? Where have I gone wrong.

Thanks
Mych
 
[1] >I get no errors but IE hangs when the function starts.
I don't think it is specifically IE problem; it is across the board.

[2]>Have I created an endless loop? Where have I gone wrong.

[2.1] The wrong is at the conditional checking.
>for (i = 5; i = Grp; i--)
[tt]for (i = 5; i [red]>[/red]= Grp; i--)[/tt]

[2.2] The reason can best illustrated in this demo. Essential is that during the evaluation of the "conditional", it is just like making the return equal to the assign i=Grp, making it effectively:
[tt]returnValue = (i = Grp);[/tt]
being equivalent to
[tt]returnValue = i = Grp;[/tt]
With Grp=2, the loop would always recognize it as true. (Try if Grp=0, then no loop at all, as it would be recognized as always false.)
[tt]
<script language="javascript">
function doit() {
for (var i=5;i=2;i--) { // [red]warning![/red] //conditional 2 (as true) with i=2 as side-effect; loop endlessly
//for (var i=5;i==2;i--) { //this will never loop as i==2 compare will return false
//for (var i=5;i>=2;i--) { //the correct line
//for (var i=5;i=0;i--) { //the conditional return 0 (as false) with i=0 as side-effect; never loop
alert (i);
}

var b,j;
b=(j=2); //effect is b=j=2;
alert (b+"\n"+j);
b=(j=0); //effect is b=j=0
alert (b+"\n"+j);

alert (c=5); //return 5
}
window.onload=doit;
</script>
[/tt]
 
OK..... I need marching out and stood infront of a firing squad.... Must remember COMPARATORS....

Above code should have

for (i = 5; i [red]==[/red] Grp; i--)

Tried this and now no hangs, no errors but still not working.

My test has groups 1 to 4 filled with data. I click the move down in group 2 and expect the following

Data from group 5 to go to group 6 (even though both contain no data)
Data from group 4 to go to group 5 etc etc
Finaly data from group 2 to go to group 3 and then
Group 2 being made blank.

What I get is all data remains in place except group 2 which is made blank.

Any ideas?
 
That I had explained. Read again what I tried to say in [2.1] as a straight answer. If you want to see the bigger picture, it is [2.2].
 
Thanks Tsuji,,,, spotted my comparator mistake just as you did... but see above still have a problem
 
Do you mean even with the [highlight]>[/highlight]=?
[tt] for (i = 5; i [highlight]>[/highlight]= Grp; i--)[/tt]
 
Tsuji

Thank... all OK. It took a while to sink into my head but now I understand why you used >=...

Thanks for your help

Mych
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top