13 January 2023

In vanilla JavaScript, the equivalent of the jQuery click function would be the onclick property. The onclick property in JavaScript is an event property of HTML elements that allows you to specify a function to be called when an element is clicked. When you use onclick property in JavaScript it will overwrite any previous onclick function, you might want to look into addEventListener, it appends a function.

Source code viewer
  1. // Single event per element.
  2. document.getElementById("element_id").onclick = function() {
  3. // Code to be executed when the event is triggered.
  4. };
  6. document.querySelector(".my_class").click();
  8. // Multiple events per element with eventListener (add, trigger, remove).
  9. element.addEventListener('click', handleClick);
  11. var event = new Event('click');
  12. element.dispatchEvent(event);
  14. element.removeEventListener('click', handleClick);
Programming Language: Javascript