I want to simulate click on GMail COMPOSE button using JS without JQuery.
Here is button:
<div class="T-I J-J5-Ji T-I-KE L3" role="button" tabindex="0" gh="cm"
style="-webkit-user-select: none;">COMPOSE</div>
Here is my js:
var element = document.getElementsByClassName('T-I-KE')[0];
element.click();
Result:undefined
in all browsers
Image: http://i.imgur.com/4IX9DZX.png
Already tried that:
var event = document.createEvent("MouseEvent");
event.initEvent("click",true,true);
var element=document.getElementsByClassName("T-I-KE")[0];
element.dispatchEvent(event);
Result:true
. But nothing happens.
Image: http://i.imgur.com/pwVqukP.png
- GeeksforGeeks How to simulate a click with JavaScript ? Method 1: Using the click () method: The click () method is used to simulate a mouse click on an element. It fires the click event of the element on which it is called. The event bubbles up to elements higher in the document tree and fires their click events also.
The event bubbles up to elements higher in the document tree and fires their click events also. The element to be clicked first selected and the click () method is used. This simulates a click on the element.
If you Google this, you might get excited to discover that vanilla JavaScript has a click () method. The HTMLElement.click () method simulates a mouse click on an element.
with JavaScript ? Click Me! Method 2: Creating a new CustomEvent: The CustomEvent constructor is used to create a new event to be used on any element. The CustomEvent interface handles the events initialized by an application for any purpose. The ‘click’ event can be passed to the constructor of CustomEvent to create a click event.
After two days i am finally got an answer!
That button listens mouseDown and mouseUP events. Working code:
var down = new MouseEvent('mousedown');
var up = new MouseEvent('mouseup');
var elem = document.getElementsByClassName("T-I-KE")[0];
elem.dispatchEvent(down);
elem.dispatchEvent(up);
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