Reputation: 3368
I am attempting to get the value of specific looped elements. I basically have a list of records and if I click on a record, I want to be able to obtain the value of the status
of that element. The status
value is binary, either 0 or 1. The issues I am running into are the following:
-The click function I created is not generating a value and the alert is not showing.
-As you can see in my foreach
loop, I am outputting the $status_img
for $status
if it meets a certain condition. The value I need is really $status
.
Does anyone see what I can do to help my problems?
foreach ($rows as $row) {
$status = $row['status'];
$class = $status != 0 ? 'status-nonzero' : '';
if ($status == 0) {
$status_img = '<img src="../icons/collection/x-sign.png" alt="Goal Not Complete">';
}
else {
$status_img = '<img src="../icons/collection/checkmark.png" alt="Goal Complete">';
}
$goal_date = $row['date'];
$fixed_goal_date = fixDate($goal_date);
$html = "";
$html .= '<div class="goal-box" id="comment-'.$row['id'].'">';
$html .= '<div class="goal-box-left">';
$html .= '<div class="goal-post-status">'.$status_img. '</div>';
jQuery:
$('.goal-post-status').click(function (event) {
var status = $(this).val;
alert(status);
});
Upvotes: 0
Views: 39
Reputation: 439
Try this:
$(".goal-post-status").click(this, function() {
var status = $(this).html();
alert(status);
});
Upvotes: 0
Reputation: 1658
You can set status in data-attribute then read it from jQuery:
HTML
$html .= '<div class="goal-box" id="comment-'.$row['id'].'">';
$html .= '<div class="goal-box-left">';
$html .= '<div class="goal-post-status" data-status="'.$status.'">'.$status_img. '</div>';
Javascript
$("body").on("click", ".goal-post-status", function (event) {
var status = $(this).attr("data-status");
alert(status);
});
Upvotes: 1
Reputation: 2436
You can use:
use $(this).html()
in place of $(this).val()
$('body').on('click','.goal-post-status',function (event) {
var status = $(this).html();
alert(status);
});
Upvotes: 0