Here is the code:
Note I can populate the cells no problem with the new order after you
dragged and dropped the fields.
Click on one of thr rows from the table at the top. It should populate
the fields.
Try dragging the text in field 1 to field 3. You notice that field 1
is missing the text.
I basically need all fields to be populated after the drag drop.
When posting code, don't use tabs, use spaces and indent two (my
preference) or four spaces for each indent.
I created a test case using simplified code based on what I think your
code was doing. Correct me if I'm wrong:
A click on the table row populates the text inputs. Dragging text in
the inputs changes the order in the table row. The text inputs are then
updated from the values in the table - correct?
The problem comes at the end - when you go to read the values from the
table and put them into the fields, the browser isn't ready so some of
the value are missing. You can either use the array of stored values to
update the text fields, or use setTimeout() to delay the text field
update (10ms seems to work fine).
My test code is below.
<html>
<head>
<script language="JavaScript">
The language attribute is depreciated, type is required:
var rowClicked;
var sourceIndex=-1;
var currentCarrierList=new Array();
var newCarrierList=new Array();
You can use initialisers here, saves a few keystrokes:
var currentCarrierList=[];
var newCarrierList=[];
var selectedCarrier;
function setSourceIndex(index){
sourceIndex=index;
selectedCarrier=currentCarrierList[index];
}
function orderCarrierChoices(targetIndex){
if(sourceIndex>-1){
//alert("running!!");
var index=0;
for(var x=0;x<currentCarrierList.length;x++){
if(x==targetIndex){
//alert("if "+x+"=="+targetIndex);
//alert("1:"+currentCarrierList[x]);
newCarrierList[index]=currentCarrierList[x];
index++;
//alert("2:"+selectedCarrier);
newCarrierList[index]=selectedCarrier;
index++;
}
else if(sourceIndex!=x){
//alert("else x="+x);
newCarrierList[index]=currentCarrierList[x];
index++;
}
}
}
//alert("x");
//document.all.choice1.value=""; [...]
currentCarrierList=newCarrierList;
*/
//alert("y");
Including huge slabs of commented-out code just wastes space and makes
it harder to find the problem.
}
function setCarrierChoices(index){
//alert("newCarrierList.length:"+newCarrierList.length);
for(var x=0;x<newCarrierList.length;x++){
//alert(x+" carrier:"+newCarrierList[x]);
if(x==0){
document.all.choice1.value="";
Even though this is IE-only code (all that ondrag stuff), don't use
document.all, use DOM compliant document.getElementById (you may need to
include a document.all alternative to cover browsers from IE 5, but I
think IE 5.5 onward supports it, certainly IE 6 does).
[...]
function resetRowValues(){
//alert("setFields:"+row.cells(0).innerText);
innerText is a Microsoft proprietary property, the DOM equivalent is
textContent but it's in DOM 3 so not widely supported. You can use
childNode values, or innerHTML and strip out any HTML tags with a
regular expression.
[...]
function select()
{
document.Testform.Code.focus();
document.Testform.Code.select();
To reference the body element, use:
var theBody = (document.documentElement||document.body);
There appears to be no variable or element with either NAME or ID of code.
}
</script>
</head>
<body name="Testform">
There is no name attribute for the body element.
<div id="divSelectDragItem1" onmouseout"alert('rowmouseleave')"
style="position:absolute; top:140; left:50; width:100;
When specifying lengths in styles, you must use units - e.g. em, ex, px.
[...]
Here is some play code:
<html><head><title>Drag n Drop test</title>
<script type="text/javascript">
var startEl;
var endEl;
var valueRow;
var valueValues = [];
function initDragEvents()
{
document.body.ondragstart = function() {
startEl = event.srcElement;
}
document.body.ondrop = function() {
endEl = event.srcElement;
doDrop();
}
}
function doDrop()
{
if ( startEl && startEl.name
&& /input-/.test(startEl.name)){
var startIdx = startEl.name.split('-')[1];
}
if ( endEl && endEl.name
&& /input-/.test(endEl.name)){
var endIdx = endEl.name.split('-')[1];
}
swapArrayValues(valueValues, startIdx, endIdx);
loadRow(valueRow, valueValues);
setTimeout('loadFields(valueRow)',10);
startEl=null;
endEl=null;
}
function swapArrayValues(A, x, y)
{
var tmp = A[x];
A[x] = A[y];
A[y] = tmp;
}
function loadRow(r, A)
{
var i=A.length;
while(i--){
delKids(r.cells
);
r.cells.appendChild(document.createTextNode(A));
}
}
function delKids(el)
{
while(el.firstChild) el.removeChild(el.firstChild);
}
function loadFields(r)
{
var tCells = r.cells;
valueRow = r;
var f = document.forms['formA'];
for (var i=0, j=tCells.length; i<j; ++i){
valueValues = tCells.firstChild.data;
f.elements['input-'+i].value = valueValues;
}
}
window.onload=initDragEvents;
</script>
</head>
<body>
Click the row to put the values into the text fields
<table border="1">
<tr onclick="loadFields(this);">
<td>AAA</td><td>BBB</td><td>CCC</td>
</tr>
</table>
<form action="" name="formA">
<input type="text" name="input-0">
<input type="text" name="input-1">
<input type="text" name="input-2">
</form>
</body>
</html>