Say I have a click handler defined:
$("#foo").click(function(e){
});
How can I, within the function handler, tell whether the event was fired programmatically, or by the user?
Say I have a click handler defined:
$("#foo").click(function(e){
});
How can I, within the function handler, tell whether the event was fired programmatically, or by the user?
I have tried all above solutions.Nothing has been worked in my case. Below solution worked for me. Hope it will help you as well.
$(document).ready(function(){
//calling click event programmatically
$("#chk1").trigger("click");
});
$(document).on('click','input[type=checkbox]',function(e) {
if(e.originalEvent.isTrusted==true){
alert("human click");
}
else{
alert("programmatically click");
}
});
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.min.js">
</script>
<input type="checkbox" id="chk1" name="chk1" >Chk1</input>
<input type="checkbox" id="chk2" name="chk2" >Chk2</input>
<input type="checkbox" id="chk3" name="chk3" >Chk3</input>
Vanila js solution:
document.querySelector('button').addEventListener('click', function (e){
if(e.isTrusted){
// real click.
}else{
// programmatic click
}
});