KaviSuja KaviSuja - 1 year ago 90
HTML Question

For each loop get td value on click event jQuery

I have a table in my view page, which display a list of values using foreach as following:

<tr>
<td >
@Html.DisplayFor(modelItem => list.First().Project_Name)
@Html.Hidden("TProjectId", list.First().Project_Id, new { id = "pro" + i })
</td>
<td >
@Html.DisplayFor(modelItem => list.First().Task_Name)
@Html.Hidden("TTaskId", list.First().Task_Id, new { id = "Tsk" + i })
</td>

@foreach (var dateitem in list)
{
if (dateitem.Date.DayOfWeek == DayOfWeek.Sunday || dateitem.Date.DayOfWeek == DayOfWeek.Saturday)
{
<td >
<input id="Day1" name="Day1" type="text" value="" disabled="disabled" />
</td>
}
else
{
<td id="HoursTxt">
@Html.TextAreaFor(modelitem => dateitem.Hours, new { id = string.Format("txtHours") })
@Html.Hidden("CuDate", dateitem.Date)
<img src="~/Images/comment.png" class="prevtest" />
<div style="border:solid;display:none;">
@Html.TextAreaFor(modelItem => dateitem.Notes, new { id = "TxtNotes" })
</div>
</td>
}
}
</tr>


I want to get date(hidden field) while clicking on image in this...
My jquery is like following:

$('.prevtest').on('click', function () {
$(this).next().toggle();
var id = $('#CuDate').val(); alert(id);
var ProjId = $(this).closest("tr").find("#TProjectId").text(); alert(ProjId);
});


I got the first date only when I click on any images.
For project id in hidden field how can i get it? currently empty data return
Can anyone help me to do this..

Answer Source

hidden element is prev sibling of clicked image. thus you can traverse to the element using .prev() selector along with clicked element context this:

$('.prevtest').on('click', function () {
    $(this).next().toggle();       
    var id = $(this).prev().val(); 
    alert(id);
});
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download