I'm not too sure if this is possible, but is there anyway of achieving this? I want to have some basic protection from scripts that will automatically register click events on my buttons, e.g.: pesky bots.
I want to only allow clicks with the mouse, not clicks triggered by javascript itself.
Any ideas or other methods of protection against this?
An element receives a click event when a pointing device button (such as a mouse's primary mouse button) is both pressed and released while the pointer is located inside the element.
click() method simulates a mouse click on an element. When click() is used with supported elements (such as an <input> ), it fires the element's click event. This event then bubbles up to elements higher in the document tree (or event chain) and fires their click events.
Your answerThis is not possible; pseudo-elements are not part of the DOM at all so you can't bind any events directly to them, you can only bind to their parent elements.
Checking if a element is clickedgetElementById() method, then add a click eventListener to it. In the code above, we have added an alert element is clicked inside the addEventListener() method. So, if a user clicks on the div element it shows an alert inside the browser that element was clicked.
You want to identify that the click event has triggered through element click or through any js code, right?
In that case you can use "event" object returned by "click" event
You can use
event.hasOwnProperty('originalEvent')
above statement returns true if the event is triggered by clicking on target element else returns false
Note: Note sure how foolproof is this.
There is an originalEvent property that will be set when the user triggers the click event, so you can check it I think
$('div').click(function(e){
console.log(e, this, e.originalEvent)
if(e.originalEvent){
console.log('user clicked')
}
}).click();
Demo: Fiddle
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With