How to trigger click event without clicking JavaScript | Example Code

If you want native JS trigger click event without clicking then use element id and click() method of JavaScript.

Syntax: JavaScript trigger click on element

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

Trigger click event without clicking in JavaScript Example

Complete HTML example code:

Create one button with “trigger” id and assign msg() function to onClick attribute. If button click then msg() function trigger an popup alert.

<html>  
<body>  
	<button id="trigger" onclick="msg()">Auto Click</button>

	<script>
		document.getElementById("trigger").click();

		function msg(){
			alert("Auto click");
		}
	</script>

</body>  
</html>  

Output:

trigger click event without clicking JavaScript

Do comment if you have any doubts and suggestion on this JS event topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.