Reputation: 175
Please excuse formatting on this but we can not connect to and post to any public sites so this is posted from my phone.
We have an ASP.Net gridview with checkboxes. We have logic that will look for what values are checked or not. The only issue we have is that we have one case that opens another window with a new page based on those check boxes. The user might have checked more check boxes since the last post back so we are going through the gridview to check that page of data and get checked values.
I can get the checked boxes with no problems. The problem is I am not sure how to find the value from another column.
I can not post much code, but I did get permission to post this much.
function myfunctionname (gridviewid){
var myvalue = "";
$('#' + gridviewid + ' input[type=checkbox]:checked').each(function (index){
myvalue = myvalue + $(this).IDontKnowWhatINeedHere
});
}
I have tried .find()
, .parente()
, .name()
and many other things. Basically I always have undefined, a jQuery function, or an error no matter what I have tried.
What I am trying get for is, since I have the correct checkbox, I want to get the parent row and get the value from a label generated as a span in another column.
Anyone have any ideas where to go from here to get to my next step?
Thanks
Upvotes: 1
Views: 13085
Reputation: 325
Without seeing the code I could guess at a possible solution. The jquery function "closest". http://api.jquery.com/closest/
this is probably a bad example but it shows how it could work or maybe help.
<div>
<p><label for="randomName">label</label><input type="text" name="randomName" id="randomName"/> </p>
<p><label for="randomName1">label</label><input type="text" name="randomName1" id="randomName1"/> </p>
</div>
<script type="text/javascript">
$("[name='randomName']").closest( 'div').find( "[name='randomName1']").val();
</script>
Upvotes: 1
Reputation: 16446
You can get the text from a <span>
on another column by getting the parent <TR>
, after that, you can find the correct column by index or, more recommended, find by ID or class attribute:
...
<tr>
<td><input type="checkbox"></td>
...
<td><span>Here</span></td>
...
</tr>
...
<script type="text/javascript">
function myfunctionname (gridviewid){
var myvalue = "";
$('#' + gridviewid + ' input[type=checkbox]:checked').each(function (index){
myvalue = myvalue + $(this).closest("tr").find("span").text();
// or $(this).closest("tr").find("#MySpanID").text();
// or $(this).closest("tr").find(".MySpanClass").text();
});
}
</script>
Notice that I used closest()
. This method searches up the DOM for the first parent that it founds with the specifiled selector. In this case, it will traverse the DOM up until the parent <TR>
of the checkbox. From there you can search down for any children.
Upvotes: 1
Reputation: 61
The first problem is to find the parent row.
$(this).closest('tr');
will get you to the parent row of the checkbox. You can get the list of columns in the row like this:
var tds = $(this).closest('tr').find('td');
Then once you have that you can just use the .eq function and the index of the column you are looking for like this
var col = tds.eq(index);
Then you should be able to get the value from inside the span like this:
var spanValue = col.find('span').text();
Re: .eq() and .closest()
Upvotes: 1