0

I need a javascript bookmarklet that will click on a button within a web based program I use for work. It is just the refresh button for part of the page. The element information (from Google Chrome's developer tools) is listed below.

Refresh

I tried this: javascript:document.getElementsByClassName("Ref btn btn3d TableBtn").click();

Seems to do nothing.

I haven't messed with bookmarklets or javascript in a very long time, but I know something like this is possible.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Seatbelt99
  • 53
  • 1
  • 7
  • `getElementsByClassName` returns `HTMLCollection`, not the single element – The Reason Dec 08 '16 at 16:12
  • try using `getElementById`? http://stackoverflow.com/questions/902713/how-do-i-programmatically-click-a-link-with-javascript – Matthias Dec 08 '16 at 16:23
  • That's what I was hoping to do, but I can't find the element ID anywhere in the source code. I wasn't sure that every button would have an ID, is that the case? – Seatbelt99 Dec 08 '16 at 16:45

1 Answers1

0

You're getting an array returned to you. Trying to click on it is failing because even if there is only one matched element, you still aren't running your click function on it. You need to run your click function on that specific element by picking it from he returned array.

var button = document.getElementsByClassName("Ref btn btn3d TableBtn")
button[0].click()

The exact syntax you would use in your bookmarklet would be;

javascript:var button = document.getElementsByClassName("Ref btn btn3d TableBtn"); button[0].click();
George Kendros
  • 792
  • 3
  • 10
  • 23