Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simulate click on GMail div button with JS

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

like image 681
Mikolaytis Avatar asked Feb 12 '15 12:02

Mikolaytis


People also ask

How to simulate a click with JavaScript?

- 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.

How to simulate a click on an element in a document?

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.

Does vanilla JavaScript have a click () method?

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.

How to create a click event in JavaScript?

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.


1 Answers

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);
like image 147
Mikolaytis Avatar answered Oct 16 '22 18:10

Mikolaytis