0

How do I click this button?

html code to the button to click

I tried this in tampermonkey. if (document.getElementsByClassName("container") && document.getElementsByClassName("container").style.display != 'none'){ document.getElementsByClassName("close-button").click(); }

Anyone know what's wrong?

  • 2
    You need to use `addEventListener` instead of `click`. There's no such function as `click`. Also getElementsByClassName returns a **list** of elements. You need to loop over them and add the event listener to each one individually. You can find plenty of examples of this process already online if you search – ADyson May 20 '21 at 16:09
  • Have a look at what getElementsByClassName returns. https://stackoverflow.com/questions/10693845/what-do-queryselectorall-and-getelementsby-methods-return – Zam Abdul Vahid May 20 '21 at 16:11
  • @ADyson No [click](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/click)? – Teemu May 20 '21 at 16:24
  • 1
    @Teemu I meant as an event handler :-). But in retrospect perhaps that's not what was being asked... – ADyson May 20 '21 at 17:25

2 Answers2

0

Try using

document.querySelectorAll("container")
  forEach((element) =>

instead of

document.getElementsByClassName("container")

and addEventListener

idz
  • 3
  • 5
  • Most likely `container` tag doesn't exist in OP's markup. – Teemu May 20 '21 at 16:14
  • Hello, I only want this button searched for once at the start when I load the page and never again after that. It's a popup pretty much with a close button on it. It's inside a javascript game. Does "addEventListener" still apply to me? There are other lines in the code that click buttons fine, but they have ID's, only this one doesn't. Also when I add the code above, the code below this gives a parsing error. – user8499005 May 21 '21 at 06:26
  • Does it matter if the thing I want to close is inside an iframe? – user8499005 May 21 '21 at 07:22
0

EDIT - Some confusion around here without some more care ...

  • There is a jQuery method .click() that binds a click event handler to an element but not invokes that event
  • There is a JavaScript method .click() that invokes a click event on an element but not handles that event

Obviously aimed here is binding / defining an event handler with JavaScript but thinking jQuery.

In JavaScript, if you have only one element with class "close-button", then you can refer this element directly with getElementsByClassName("close-button")[0] and might fiddle with "dispatchEvent" on this.

hh skladby
  • 101
  • 6
  • [click](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/click) is very native ... – Teemu May 20 '21 at 16:25
  • @Teemu Got me! Too eager to see jQuery traces everywhere ... – hh skladby May 20 '21 at 16:31
  • Nothing happens when I do that. – user8499005 May 21 '21 at 08:40
  • @user8499005 Does my edit of answer help? – hh skladby May 21 '21 at 10:29
  • This is my first time trying to code. All I did was look at the current code someone else wrote, and copy the action. I tried this in the console and it worked. let mayhem = document.querySelectorAll('.close-button') mayhem.forEach(btn => btn.click()) But when I use it in tampermonkey, nothing happens. (function() { 'use strict'; window.addEventListener("load", function(event) { console.log("All resources finished loading!"); }); let mayhem = document.querySelectorAll('.close-button') mayhem.forEach(btn => btn.click()) })(); – user8499005 May 21 '21 at 13:53
  • @user8499005 In the console there's all the stuff loaded by the page you opened the console in, maybe jQuery. For trying out code you should use one of the many IDE like playground-sandboxes available today online for free, eg. https://jsfiddle.net/, https://codepen.io/, https://codesandbox.io/, and http://stackblitz.com/. – hh skladby May 21 '21 at 16:36
  • @user8499005 I've never heard of tampermonkey till your mentioning, but it seems to be something like the legendary Greasemonkey - these are to manipulate what your browser does with webpages and add some functionality. On Stack Overflow you should set and search the tags "tampermonkey", "userscripts", and "greasemonkey" to get help - with tagging as "javascript" you are running into webdev people with totally different backgrounds; they may not understand you and you may not understand them. – hh skladby May 21 '21 at 16:48