How to click non-clickable HTML tags? Via console

Asked

Viewed 211 times

0

Good staff I am with a doubt, I would like to click on non-clickable HTML elements, as for example

I know that with the following code we can click a button

knowing that I am working via console in the browser

document.element.click()

Whereas

element = <a>

Now as I would if I were a div or other HTML tag?

  • 1
  • 1
  • And why would you like to click on a div, that has no action to click? What do you expect that click "artificial" make?

  • So it’s out of curiosity because I’m on a page and I realized that the field is editable only when I click on the component, but I realized that the answer is to work with events and not manipulating the DOM as I was trying to do. @Luizfelipe.

  • 1

    Thank you so much for the links @Icaromartins I think with them I’ll get what I want.

1 answer

1


The big thing is that you can add Event Listener to a 'click' on any html element.

In your case, a div, just add the event’s Function Handler:

const divElement = document.querySelector('#idDaDiv');
divElement.addEventListener('click', () => alert('clicou'));

And then, whenever you want, trigger the event as you yourself mentioned above:

//...
divElement.click();

Or trigger the event using some of the approaches of the other responses.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.