View Full Version : Traverse a HTML table and read cell values
chino8
12-21-2003, 12:45 PM
Hi. I have a dynamically created Html table with values retrieved from a db table into text input fields and at the end of each Row, I have a checkbox field.
I want the user to edit and check those rows that needs to be updated in the database, once submit button is clicked.
So my problem is that I don't know how I can go through each row of the table, evaluate if the checkbox is marked and if it is, I want to send the textbox values to another function that will update the database (this last part I know how to...)
My guess is this is to be done with JSP, and any type of help/advice will be greatly appreciated.
Thanks!
hyun chang
Rydberg
12-21-2003, 01:45 PM
I'm not exactly a JSP guru(yet), but here goes...
Firstly, your checkboxes would have to look something like this:
<input type="checkbox" name="datalist" value="record id" />
Where record id is the id of the records you've pulled out of the database. The input should then look like this:
<input type="text" name="record id" value="record value" />
Then point the form to a servlet that will process the form. In doPost(), you can then reach the checked boxes like this:
String[] checkedItems = req.getParameterValues("datalist");
Now you have all the affected record ids in the checkedItems array, as strings. Now, to get the new values from the textboxes:
String[] checkedValues = new String[checkedItems.length];
for (int j = 0; j < checkedItems.length; j++) {
checkedValues[j] = req.getParameter(checkedItems[j]);
}
The array checkedValues now contains those values.
That should get you going, I think.. Just send these arrays to the function that updates the database and loop through them, to update the records.
HTH
Note: JSP is server-side technology, so this post belongs in the Server side programming forum.
chino8
12-21-2003, 02:58 PM
Thanks for the prompt response. Though your suggestion is very useful, I need this to be run on the client side, and I managed to find the way to go through each Table Row, and get the Table cell's Input field value via DOM methods:
for loop..
document.getElementById(id).getElementsByTagName("TR").item(i).getElementsByTagName("input").item(j).getAttribute("value")..
Now, my problem is to find out which row's checkboxes have been checked, as the above approach, only returns the value of the input tag, and checkboxs have a static value:
exampe: <input type=checkbox name=checkboxupd value=N unchecked> and when it is checked the value will not change..and therefore with the DOM method, I will always get N.
Thougth about doing an onchange method, to update when it gets clicked, but doesn't work...Any idea/suggestion will be more than welcome!
Thanks again,
hyun chang
Rydberg
12-21-2003, 03:22 PM
I suppose you mean JavaScript(JS) and not JavaServer Pages(JSP) then. Why you would want that done on the client side and not the server side, is beyond me, really.
Someone will surely help you solve your problem.
Why do I bother :rolleyes:
Jon Hanlon
12-21-2003, 04:56 PM
You're nearly there.
for loop..
nextGuy = document.getElementById(id).getElementsByTagName("TR").item(i).getElementsByTagName("input").item(j);
if (nextGuy.checked == true) ....
You need to examine the checked (http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/checked.asp) property, not the value.
vBulletin® v3.6.7, Copyright ©2000-2009, Jelsoft Enterprises Ltd.