How to click on td element?

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

Does not work. Apparently such an element cannot be clicked by a script, is there a crutch of some kind?

Google did not help

upd: It is necessary to initiate a click on the item, the site opens in JavaFX WebView.

upd2: if you click in the browser console, returns undefined

  • jsfiddle.net/d5aotfqx everything works for me - user33274
  • I need to click on it, the function on the site is already set - jessez
  • You can launch a mouse event of clicking the coordinates of td (middle)? fireEvent(new MouseEvent(...)) in JavaFX? or in javascript event.initMouseEvent(...) ? - Sergey
  • @Sergey thanks for the tip, I'll try and write it off - jessez
  • @Sergey something will not reach me how to get the coordinates of the element .. - jessez

3 answers 3

The fact is that, unlike a simple function call, events have phases of ascent or tunneling (in this case only ascent). Therefore, it is not enough to cause a simple execution of a function, as it is done with click() , since higher levels may still have interceptors.

Found the answer: http://darktalker.com/2010/07/manually-trigger-dom-event/

 /** * trigger a DOM event via script * @param {Object,String} element a DOM node/node id * @param {String} event a given event to be fired - click,dblclick,mousedown,etc. */ var fireEvent = function(element, event) { var evt; var isString = function(it) { return typeof it == "string" || it instanceof String; } element = (isString(element)) ? document.getElementById(element) : element; if (document.createEventObject) { // dispatch for IE evt = document.createEventObject(); return element.fireEvent('on' + event, evt) } else { // dispatch for firefox + others evt = document.createEvent("HTMLEvents"); evt.initEvent(event, true, true); // event type,bubbling,cancelable return !element.dispatchEvent(evt); } } 

This code already causes the event to run fully, along with the ascent phase.

Example:

 var fireEvent = function(element, event) { var evt; var isString = function(it) { return typeof it == "string" || it instanceof String; } element = (isString(element)) ? document.getElementById(element) : element; if (document.createEventObject) { // dispatch for IE evt = document.createEventObject(); return element.fireEvent('on' + event, evt) } else { // dispatch for firefox + others evt = document.createEvent("HTMLEvents"); evt.initEvent(event, true, true); // event type,bubbling,cancelable return !element.dispatchEvent(evt); } } $(document).ready(function(){ $("td").click(function(){ $(this).toggleClass("red"); }); $("table").click(function(){ $(this).toggleClass("blue"); }); // Trigger the event. fireEvent($("td")[0], 'click'); }); 
 td{ padding:30px; border:1px solid #000; } .red{ background:red; } .blue{ background:blue; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tr> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> </tr> </table> 

  • Thank you very much, but this is not what I needed! I have already found the solution, thanks again - jessez

After Google’s two days of torture, I accidentally found a clue that helped me. We managed to click on an element by completing the next two lines of code

 browserEngine.executeJavaScript("document.getElementById('event5808638win1').onmousedown();"); browserEngine.executeJavaScript("document.getElementById('event5808638win1').onmouseup();"); 
  • "The next two lines of code." Do not invent extra terms :) - D-side
  • @ D-side thanks :)) - jessez

Use jquery:

  $('table').on('click', 'td', function(){ alert("sdffsd"); }); 
  • I need to click on an element, through a webview in java. I want to write a bot, but I can not click on the element. - jessez
  • It monitors events, but does not trigger them. - D-side
  • @jessez indicate this in the question. Perhaps from Java / WebView it will be made even easier. - D-side
  • @ D-side pointed, thanks. But I doubt that it will be really easier. Google is not the first day, I did not find anything that can help. If you click in the browser console, it returns undefined - jessez