I'm using JQuery I have a table inside a razor view... I want to get a value(StudentID) according to a click on an img that I have in the same row, I want to get the value and then send it to an Action Method in the Controller class as a parameter.
With this sentence I'm just getting an string, for example if a click the img element and in that row the value of StudentID is 129, then the value of id is
"\n 129 \n"
I'm expecting id to have the value 129.
This is the sentence, what I need to change? I've tried using innertext() but JQuery doesn't recognize it
var id = $(this).parent().siblings().eq(3).text()
If you want to consult the table this is:
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.LastName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Age)
</td>
<td style="display:none" id="tdStudentID">
@Html.DisplayFor(modelItem =>item.StudentID)
</td>
<td>
<img src="~/images/deleteIcon.png" width="20" height="20" class="imgJQuery" />
</td>
</tr>
}
var id = $(this).closest('tr').find('#tdStudentID').text()var id = $(this).closest('tr').find('#tdStudentID').text()i would also suggest to use class instead of ID as you loop you will create duplicated ID andID should always be UNIQUE. so your selector will bevar id = $(this).closest('tr').find('.tdStudentID').text()"\n 129\n "I tried using the selector you gave me but it's getting the same result<img src="..." class="imgJQuery" data-id="@item.StudentID" />and then$('.imgJQuery').click(function() { var id = $(this).data('id'); ..... });