I have a table that generates a buttons dynamically and I would like to change the colour and the text when I execute a function onclick. The text is modified correctly but when execute the function all buttons change the colour at the same time.
I use bootstrap 3 and the structure on my server is:
- index.php -> I load css, scripts, jquery and my functions.
- ajax/readRecords.php --> I generate the mysql table
- js/script.js --> my function onclik
- js/jquery.js --> the jquery
The php code:
while($row = mysqli_fetch_assoc($result))
{
$status = $row['register_status'];
if ($status == "Active") {
$register_status = '<button class="btn btn-success btn_status btn-block" data-status="Active">Active</button>';
}
else {
$register_status = '<button class="btn btn-warning btn_status btn-block" data-status="Inactive">Inactive</button>';
}
$data .= '<tr>
<td width="1%"><mark>#'.$row['id'].'</mark></td>
<td>'.$row['first_name'].'</td>
<td>'.$row['last_name'].'</td>
<td>'.$row['email'].'</td>
<td width="1%">'.$register_status.'</td>
<td width="1%">
and this is the jquery function:
jQuery(document).ready(function(){
$('body').click('.btn_status', function(e){
var button = jQuery(e.target);
if(button.data('status') == 'Active'){
button.data('status', 'Inactive');
button.html('Inactive');
button.removeClass("btn-success").addClass("btn-warning");
}else if(button.data('status') == 'Inactive'){
button.data('status', 'Active');
button.html('Active');
button.removeClass("btn-warning").addClass("btn-success");
}
});
})
I sense that the value of the class is lost but can't know what's the solution. thanks!