1
I have the following toggle:
It works perfectly, but I would like to put it in a way that works individually for each record that comes from the comic book, but I’m not getting it. Look at:
CSS
<style type="text/css">
.onoff input.toggle {
display: none;
}
.onoff input.toggle + label {
display: inline-block;
position: relative;
box-shadow: inset 0 0 0px 1px #d5d5d5;
height: 30px;
width: 70px;
background: #DC143C;
border-radius: 30px;
}
.onoff input.toggle + label:before {
cursor: pointer;
content: "\00a0 \00a0 \00a0 \00a0 \00a0 \00a0 \00a0 Não";
display: block;
color: #FFF;
height: 30px;
border-radius: 30px;
background: rgba(19, 191, 17, 0);
transition: 0.1s ease-in-out;
}
.onoff input.toggle + label:after {
content: "";
position: absolute;
height: 30px;
width: 30px;
top: 0;
left: 0px;
border-radius: 30px;
background: #fff;
box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.2), 0 2px 4px rgba(0, 0, 0, 0.2);
transition: 0.1s ease-in-out;
}
.onoff input.toggle:checked + label:before {
cursor: pointer;
width: 70px;
content: "\00a0 \00a0 \00a0 Sim";
color: #FFF;
font-family: 'Arial';
background: #13bf11;
}
.onoff input.toggle:checked + label:after {
left: 40px;
box-shadow: inset 0 0 0 1px #13bf11, 0 2px 4px rgba(0, 0, 0, 0.2);
}
#estado{ font-size: 14px; font-weight: bold;}
</style>
PHP
<table class="table table-striped">
<thead>
<tr>
<td>Nome</td>
<td>Ativo?</td>
</tr>
</thead>
<tbody>
<?php
...
$c = 1;
while($pe = mysqli_fetch_object($sql))
{
?>
<tr>
<td><?php echo $pe->Nome; ?></td>
<td>
<div class="onoff">
<input type="checkbox" data-id="<?php echo $pe->IdCadastros; ?>" class="toggle" id="onoff">
<label for="onoff"></label>
<input type="hidden" name="Avisar" id="campo<?php echo $c; ?>" value="0">
</div>
</td>
</tr>
<?php
$c++; }
?>
</tbody>
</table>
JQUERY
<script type='text/javascript'>
//<![CDATA[
window.onload=function(){
var onoff = document.getElementById('onoff');
onoff.addEventListener('change', function() {
var id = $(this).attr('data-id');
estado = this.checked ? 'S' : 'N';
var campo = document.getElementById("campo1").value = estado;
$.ajax({
url: '<?php echo $caminhoAbsoluto; ?>/status.php',
type: 'post',
data: {
estado: this.checked,
campo: campo
},success: function(data) {
//alert();
console.log(data);
}
}).done(function(msg) {
});
});
}//]]>
///////////////////
</script>
How can I make it work individually for each record?
Individually that you refer to you don’t need to use the selector per id, so you don’t have to create a Function for each id name?
– hugocsl
Hi Hugo. I made a change in my post. In the list will appear the toogle for each user. Need so that when changing the status to yes or no, pass the value of the user ID to Jquery and make the change of the status of each in the database.
– user24136
My question is more in passing the ID of each user through Toogle by Jquery, because the change in BD I am doing with PHP and everything is Ok.
– user24136
Now it became clearer the problem, but qq forna, I do not know answer rss :D
– hugocsl
kkkkkkk quiet
– user24136
Then you’ll submit the whole form or it’s just that same Ajax?
– Sam
Hi Sam. This... I made a change to my post. I used the data-id attribute. I don’t know if it’s the right way, but Paree that now just need to pass this value by url.
– user24136
change this var id = $(this). attr('data-id'); so var id = $(".toggle"). attr('data-id'); and run a test
– Lucas Antonio