I am trying to read an ID from a table row in my html via a checkbox/edit button contained in the row. The row has id of “name_id#” Here is the code:
$('.EditButton').click(function edit() {
var patchID = parseInt($(this).attr('id').slice(5));
console.log("patchID is : " + patchID.toString());
...
});
I have tried using substring and slice, and without parseInt. Everything seems to grab the ID number correctly, however when I log it, it outputs this:
patchID is : 63
patchID is : NaN
Is there something totally wrong with my code? When I try and use a jquery “get” using the ID, it works, but it also throws an error because it tries to “get” with both the ID # and “NaN”.
Here is the html for my table rows:
@foreach (var patch in patches)
{
<tr id=@string.Format("patch_{0}", patch.PatchID)>
<td>
<div class="EditButton">
<input type="button" class="button EditButton"
value=""
id=@string.Format("edit_{0}", patch.PatchID) />
</div>
<div class="SaveButton">
<input type="button" class="button SaveButton hidden"
value=""
id=@string.Format("save_{0}", patch.PatchID) />
</div>
</td>
</tr>
}
Thanks for your help!
The
$('.EditButton')will match thedivandinputwith the.EditButtonclasses. Theinputis giving you the 63, whereas thedivis giving you the NaN since it doesn’t have anidattribute.I would remove the
.EditButtonclass from the div (unless you have it for styling) to prevent the click handler from being called twice.