AlexGH AlexGH - 1 year ago 89
HTML Question

Get value from table using JQuery

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

is 129, then the value of

"\n 129 \n"

I'm expecting
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) {
@Html.DisplayFor(modelItem => item.Name)
@Html.DisplayFor(modelItem => item.LastName)
@Html.DisplayFor(modelItem => item.Age)
<td style="display:none" id="tdStudentID">
@Html.DisplayFor(modelItem =>item.StudentID)
<img src="~/images/deleteIcon.png" width="20" height="20" class="imgJQuery" />


Answer Source

Add student id as data attribute on the image instead of putting it inside a hidden td, like

<td> <img src="~/images/deleteIcon.png" width="20" height="20" class="imgJQuery" data-student-id='@Model.StudentID'/> </td>

Then simply catch it like that

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download