Forever, we have been using javascript functions to pass data from a table to whatever we need to do with that row of data. For example, take this table:
<table class="table table-hover table-striped">
<thead>
<tr>
<th>Name</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr>
<td>Jack</td>
<td><a href="javascript:myfunc(1,'Jack')">edit</a></td>
</tr>
<tr>
<td>Dan</td>
<td><a href="javascript:myfunc(2, 'Dan')">edit</a></td>
</tr>
<tr>
<td>Mark</td>
<td><a href="javascript:myfunc(3, 'Mark')">edit</a></td>
</tr>
</tbody>
</table>
Normally, we would just have the following function to do something with the data that changes by row (typically passing an id, but sometimes there could be a bunch of args in the function). This function could do a variety for things - not just these examples of course.
function myfunc(f_ID, f_Name) {
alert(f_ID + "_" + f_Name);
$.colorbox({ iframe: true, overlayClose: true, opacity: .70, inline: false, fixed: true, title: "Colorbox Title", href: "/mypage.asp?id=" + f_ID + "&name=" + f_Name });
}
OR
function myfunc(f_ID, f_Name) {
if (confirm("Are you sure that you would like to permanently delete id # " + f_ID + " for " + f_Name + "?")) {
window.location = "/mypage.asp?id=" + f_ID + "&name=" + f_Name
}
}
I know that it's probably not the best to mix Jquery and old school JS like this (maybe it's fine??), so there must be a better way to access this data.
I was thinking that I could use "data-" tags, but the issue is how do you actually get the value when there are multiple rows? I cannot use an ID since it's unique and if I added a class, how do I access the data from the call?
Any help would be greatly appreciated, thank you!
UPDATE
This worked as the answer for my sitaution. Thanks @Quentin!
<a class="testlink" href="#" data-id="1234">edit</a>
$("a.testlink").on("click", myAction);
function myAction(event) {
event.preventDefault();
alert($(this).data("id"));
}