I’m using jQuery 1.6.4 and I have an array of values I’m getting from an html table. What I’m trying to do is get the value from a specific td when the user clicks on the row. I used this code to get all the td values from the particular row:
jsp code:
<tr>
<s:iterator value="model.NameList">
<tr>
<td align=center><s:property value="message.substring(0,125)"/></td>
<td align=center><s:property value="timestamp"/></td>
<td align=center><s:property value="retryCount"/></td>
<td align=center><s:property value="specialId"/></td>
...
</tr>
</s:iterator> ...
specialId is the td cell value I need to get from each selected row from the click event.
//Click event code left out.
var arr = [];
arr = $(this).find('td').map(function(){
return this.innerHTML;
}).get();
Then I need to check for a particular cells value using an if block as I loop through the cells in that row. I hardcoded it as in the code below to get the value, but I need to do this dynamically.
$.each(arr, function(i, l){
if(i == 6) { ...
So I created an id for the td cell to get that value, but I’m going to need to get this particular cells value from multiple rows so the id by itself wont work. I need to handle getting the value from the row and then getting it from multiple rows if the user selects more than one row and send that value to Struts2 hidden textboxes. I’m just using one textbox to start with and that worked.
$('#textboxId').val(l); //Set the value to the id of a hidden textbox.
Am I going about this the wrong way? Since I only need one value from each row, I’m thinking there is a better way to code this, but I also need to allow for n(values) of td’s.
How can I do this?
On click of row you can get the value using something like this:
say if your textfield is like
JS Fiddle