I'm trying to set default value for the input tag with switchery plugin using the values from database with ajax, but the checked attribute doesnt update, but the update does work without the plugin, what did i do wrong? The HTML table form looks something like this :
<table>
<thead>
...
</thead>
<tbody>
@for($j=1; $j<=10; $j++)
...
<td>
<label>
<input id="day1-hours{{$j}}" type="checkbox" data-plugin="switchery">
<span>Active</span>
</label>
</td>
...
@endfor
</tbody>
</table>
Here's the ajax success function :
for(let $i=1; $i<=6; $i++){
$.ajax({
... //the POST REQUEST
success:function(data){
for($j=1; $j<=array.length;$j++){
if(data[$j-1] == "active"){
$("#day"+$i+"-hours"+$j).prop("checked",true);
}else{
$("#day"+$i+"-hours"+$j).prop("checked",false);
}
};
}
...
}
and the data from the ajax success response should be just "active" and "inactive"
Use the following code. It works for me.
HTML:
My example HTML:
JS:
I'm adding and removing
checked
attribute, changingcss
styling and re-initializing the plugin.Check it
Note: you can pre-define your
css
, I've used default style. Do it inif statement
.