943,175 Members | Top Members by Rank

Ad:
Sep 1st, 2010
0

JavaScript Change hidden field a number of times

Expand Post »
Hi
I have a JavaScript function for swapping the values of rows in a table. I have a hidden field in each row to carry a record ID. As I switch the rows the function will update the hidden field but doesn't do it correctly if I switch a number of rows. I dynamically set the values to switch when the page loads but how can I change these values when the rows are moved.
The idea is to change the order of a recordset by clicking on an up or down arrow and then save the values on the record id and sort order, the two hidden fields in each row.
Any help would be appreciated
Thank you
Warren

function swapCells(idA,idB,id1,id2)
{
var cellA=document.getElementById('menu'+idA);
var cellB=document.getElementById('menu'+idB);
if(cellA&&cellB)
{
var temp1=cellA.innerHTML;
cellA.innerHTML=cellB.innerHTML;
cellB.innerHTML=temp1;
document.getElementById('menuid'+idA).value = id2;
document.getElementById('menuid'+idB).value = id1;
}
}

<tr>
<td id="menu1">
<input name="menuid1" type="hidden" id="menuid1" value="2" />

2 - 6 - Disability<br></td>
<td width="20" align="center">&nbsp;</td>
<td width="20" align="center"><img src="../../images/page/btn_down.png" value="down" onClick="swapCells(1,2,2,1);"></td>
<td width="60" align="center">1<input name="menuorder1" type="hidden" id="menuorder1" value="1" /></td>
</tr>
</tr>
<tr>
<tr>
<td id="menu2" width="300">
<input name="menuid2" type="hidden" id="menuid2" value="1" />

1 - 1 - Life<br></td>
<td width="20" align="center"><img src="../../images/page/btn_up.png" value="up" onClick="swapCells(2,1,1,2);"></td>
<td width="20" align="center"><img src="../../images/page/btn_down.png" value="down" onClick="swapCells(2,3,1,3);"></td>
<td width="60" align="center">2<input name="menuorder2" type="hidden" id="menuorder2" value="2" /></td>
</tr>
</tr>
<tr>
<tr>
<td id="menu3" width="300">
<input name="menuid3" type="hidden" id="menuid3" value="3" />

3 - 5 - Home and Auto<br></td>
<td width="20" align="center"><img src="../../images/page/btn_up.png" value="up" onClick="swapCells(3,2,3,1);"></td>
<td width="20" align="center"><img src="../../images/page/btn_down.png" value="down" onClick="swapCells(3,4,3,4);"></td>
<td width="60" align="center">3<input name="menuorder3" type="hidden" id="menuorder3" value="3" /></td>
</tr>
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
WarrenN is offline Offline
2 posts
since Sep 2010
Sep 1st, 2010
0
Re: JavaScript Change hidden field a number of times
...
var cellA=document.getElementById('menu'+idA);
var cellB=document.getElementById('menu'+idB);
...

check 'menu' + idA there seems wrong since u relaying on menuid1, menuid2
Reputation Points: 6
Solved Threads: 19
Posting Whiz in Training
fatihpiristine is offline Offline
283 posts
since Sep 2007
Sep 1st, 2010
0
Re: JavaScript Change hidden field a number of times
Hi Thanks

I finally figured it out so here is the change in case anyone else needs to do something like this:
function swapCells(idA,idB,id1,id2)
{
var cellA=document.getElementById('menu'+idA);
var cellB=document.getElementById('menu'+idB);
if(cellA&&cellB)
{
var temp1=cellA.innerHTML;
cellA.innerHTML=cellB.innerHTML;
cellB.innerHTML=temp1;
var temp2=document.getElementById('menuid'+idA).value;
document.getElementById('menuid'+idA).value = document.getElementById('menuid'+idB).value;
document.getElementById('menuid'+idB).value = temp2;
}
}
Thank you
Reputation Points: 10
Solved Threads: 0
Newbie Poster
WarrenN is offline Offline
2 posts
since Sep 2010

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Unable to open link in parent with slideIt() script
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: On Function Call





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC