A
A
Anton2020-08-17 10:15:16
JavaScript
Anton, 2020-08-17 10:15:16

How to implement the Click() function on a dynamically created button?

Good day! Actually, in order:
1. I'm trying to write a script for a specific site that would run from the Chrome console
2. On this site, at an indefinite time, the necessary one is generated, I managed to catch it using get by class name (only the class and innerHTML), but clicking with the click() function does not work.

I understand that the whole problem is that this is a dynamic element and something needs to be done with it before clicking (), but what exactly needs to be done - there is no understanding :)

I apply what is already working (waiting for the element to appear and its Search):

let ButtonEnter;
let MyTimer = setInterval(function SearchMyButton() {
ButtonEnter = document.getElementsByClassName('EtoKnopka');
if (ButtonEnter[0] != null) {
ButtonEnter[0].click();
console.log('Кнопка найдена!');
clearInterval(MyTimer);
}
});

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
kocherman, 2020-08-17
@zloeeskimo

function eventFire(el, etype){
  if (el.fireEvent) {
    el.fireEvent('on' + etype);
  } else {
    var evObj = document.createEvent('Events');
    evObj.initEvent(etype, true, false);
    el.dispatchEvent(evObj);
  }
}

Usage:
eventFire(document.querySelector('.EtoKnopka'), 'click');

Use
document.querySelector() 
document.querySelectorAll()

instead of
document.getElementsByClassName()
document.getElementById() 
document.getElementsByTagName()

And then these methods make the code completely mammoth.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question