in my laravel project i have colorpicker input field where I want to autosave the value in database. In this section I have pasted the script which is running properly. But the problem is this is continue saving the value after the interval. But what I want?
When I click the input field then the event will fire without clicking it would not goes on.
so plz can anyone help me out how can I use the onchange event for this. I have pasted the code below plz check it out and let me know
<script>
$(document).ready(function(){
function autoSave()
{
var id =$("#hid").val();
var statscolor = $('#statscolor').val();
$.ajax({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
method:"put",
// url:"{{ route('companyStats.update', $auto->id) }}",
url:"colorstats/"+id,
data:{backgroundcolor:statscolor},
dataType:"text",
success:function(data)
{
console.log(data);
// $('#autoSave').fadeIn().text("color successfully updated");
// $('#autoSave').fadeOut(3000);
$('#autoSave').text('updatedsuccessfully');
// setInterval(function(){
// $('#autoSave').fadeOut();
// }, 5000);
/*setTimeout(function() {
// $('#autoSave').fadeOut("slow");
$('#autoSave').fadeIn().text("color successfully updated");
}, 5000 ); */
}
});
}
setInterval(function(){
autoSave();
}, 5000);
});
</script>
this is blade input field
<div class="form-group">
<label for="statscolor">Background Color</label>
<input type="hidden" id="hid" value="{{$auto->id}}" name="hid">
<input class="jscolor float-right" name="backgroundcolor" id="statscolor" value="{{$auto->backgroundcolor}}" autocomplete="off" style="background-image: none; background-color: rgb(0, 142, 255); color: rgb(255, 255, 255);" onChange="autoSave()">
<small id="autoSave"></small>
</div>
2
Answers
This answer is based on JQuery, but if you use jscolor.js (I guess) there may be a specific event. In this case, every time your color changes, your function is called. You should probably save the data when the colorpicker is closed.
Second solution
If my guess is correct, this is the solution with jscolor.
So you can add an event Listener on the .jscolor element. for Jquery you can bind on()