Okay, I have written next to no jQuery.
I have a table with rows. In one column is a checkbox, and a hidden field with a value.
I want to find all the rows with the checkboxes checked, and concatenate the respective hidden fields together.
Here is my html (edited for simplicity):
<table class="user-forms">
...
<tr>...
<td><input type='checkbox'><input type='hidden' name='hdnObjectId'></td>
</tr>
...
(Note: I am actually using a .net gridview, and the hidden field is within a templateField, so the hidden fields names will in fact be unique)
Here is a javascript function I call on a button click:
function updateFormValues()
{
var field = document.getElementById("FormObjectIDs");
var ids = "";
$('.user-forms tr').each(function (i, row)
{
var $row = $(row),
$objId = $row.find('input[name*="hdnObjectId"]'),
$checkedBox = $row.find('input:checked');
// if ($(checkedBox).length)
if ($(row).find('input:checked').length)
{
ids = ids + "," + $(row).find('input[name*="hdnObjectId"]').val();
}
});
ids = ids.substring(1, ids.length);
field.value = ids;
}
</script>
This appears to be mostly working. The problem is that for some reason, the very first row is getting "selected", even though it's not checked. I have 4 rows in my table, and it, and the one that is actually checked, are getting "selected" by my code. So, I end up with 2 ids being concatenated together. Any idea why this would be?
2nd question:I have commented out what I thought was a short-hand I could use:
// if ($(checkedBox).length)
but I have to use the fully expanded:
if ($(row).find('input:checked').length)
why is that? I thought that by defining
$(checkedBox) up above to be equal to
$(row).find('input:checked'), I could re-use it.