js trigger click event code example

Example 1: js trigger click

var element = document.getElementById('element');
element.click();

Example 2: javascript trigger click on element

document.getElementById('myElementID').click();

Example 3: js trigger click

const body = document.body;

body.addEventListener('click', e => {
  console.log('clicked body');
});

console.log('Using click()');
body.click();

console.log('Using dispatchEvent');
body.dispatchEvent(new Event('click'));

Example 4: js trigger click

<a href="#" target="_blank" onclick="javascript:Test("Test");">MSDN</a>