How can I access “StudentID” value in JavaScript function(jQuery).
HTML:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<Student.Models.vwStudent>>" %>
<div class="divClass" id="divID">
<table class="tableClass" id="tableID">
<% foreach (var item in Model) { %>
<tr class="trClass" id="trID">
<td class="tdClass">
<%= Html.TextBox("StudentID") %>
</td>
<td class="tdClass">
<%= Html.Encode(item.StudentName) %>
</td>
</tr>
<% } %>
</table>
</div>
JQuery:
$('#ShowStudentID').click(function () {
$(".tdClass").each(function () {
alert($('.tdClass').children().val());
// How do I access StudentID and StudentName here?
});
});
If you want to get the value for each td, which seems to be what you want to do, you can use the
thiskeyword within the each loop. Something like this:Edit:
To get the value of an input within the td element, you could do this within your each-loop instead:
To make it a bit more specific, you could throw in the attribute equals selector as well:
Edit 2:
To get information from both
tdelements, you could do something like this instead:You loop over the
trelements instead, select eachtdwithin it and treat them individually.Here is a working example as well: http://jsfiddle.net/Hzfz2/