DEV Community

ChuangWANG
ChuangWANG

Posted on

3 2

Generate clicking event in js

Get DOM element is javascript

document.getElementById()
document.getElementsByName()
document.getElementsByTagName()
document.getElementsByClassName()
// Parameter same to parameter css selector
document.querySelector()  
document.querySelectorAll()
// Specific for getting html element
document.documentElement()  
// Specific for getting body element
document.body()  
Enter fullscreen mode Exit fullscreen mode

Generate clicking event in js

<html>  
    <head>  
        <title>usually function</title>  
    </head>  
    <script>  

function load(){  
    //下面两种方法效果是一样的  
    document.getElementById("target").onclick();  
    document.getElementById("target").click();  
}  

function test(){  
    alert("test");  
}  

</script>  
    <body onload="load()">  
        <button id="target" onclick="test()">test</button>  
    </body>  
<html> 
Enter fullscreen mode Exit fullscreen mode

Remark.

btnObj.click() is to actually click the button with the program, triggering the button's onclick() event

btnObj.onclick() simply calls the method pointed to by btnObj's onclick, which is just a method call and does not trigger the event.

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay