I have a PHP foreach loop like this:
<?php
foreach ($student->student as $d) {
echo
'<tr>'
'<td class="StudentID">' . $d->ID . '</td>' .
'<td>' . $d->Date . '</td>' .
'<td>' . $d->Name . '</td>' .
'<td><a class="show-confirmation">'. "click to confirm" . '</a></td>' .
'</tr>';
}
?>
I am using a bootbox js for a dialog box like this:
<script>
$(document).on("click", ".show-confirmation", function(e) {
var StudentID = $('#StudentID').val();
bootbox.confirm("Confirm, student ID? " + StudentID, function(result) {
console.log('This was logged in the callback: ' + result);
console.log('StudentID: ' + StudentID);
});
});
</script>
what I am trying to accomplish is when I click on click to confirm which has class="show-confirmation", I want to get the value of $d->ID on the bootbox js confirm dialog box. Since this is a for each loop, I want to pass a new student id each time on the dialog confirm box.
On the confirm dialog, I want to see Confirm, student ID? + 15 which is the student id for that row.
What did I do?
- I added a variable
var StudentID = $('#StudentID').val();hoping to fetch the student id value from the classStudentIDeg:<td class="StudentID">but it isundefinedwhen I am accessing it.
Could someone please kindly guide me to accomplish this method?
Here's a clean way of achieving this:
You need to do the following:
dataattribute on the elements to hold data that you require in code. This way you can shown fancy formatted data to user in the element'sinnerHTMLand have data that can be directly used in code as data attribute values.<td class="StudentID">and$('.StudentID').val()will not be able to identify which was the clicked item from all the student rows. Hence if you notice I have created a class calledStudenton the parenttr.StudentIDorDateorNameand pick the data attributes you require in code.Below is a working sample: