-5

I'm trying to write a chrome extension that will simulate clicking around on a page.

However the links on the page look something:

enter image description here

I realize .click() is used for on <button ..> right? I'm guessing he has an event handler set up when something "clicks" that.

How can I simulate clicking that from jQuery?

Morgan Allen
  • 2,737
  • 4
  • 39
  • 72
  • 3
    `$('.quickAddItem-0-161').trigger('click')`. – BenM Sep 05 '18 at 15:15
  • You can haz XPath and use that instead? Click is not just for button. Click can be on anything. If you subscribe to click.. then you will get data with X,Y position on it and the element clicked. If the element has an actions, say a button, href, then it triggers the defaultAction of that element, or the function bound to it, or the onClick= attribute or any other magix found in the Eval() of that element click event – Piotr Kula Sep 05 '18 at 15:17
  • When and where in your code are you clicking the element? When and where are you binding the event listener? `.click()` should work iff you make sure [the DOM is loaded](https://stackoverflow.com/q/14028959/4642212), _then_ bind the listener, [_then_ simulate the click](https://stackoverflow.com/a/29912369/4642212). Please, [edit] your question and provide a [mcve] (this means, your _code must be in the question itself, as [formatted text](https://meta.stackoverflow.com/q/251361/4642212) instead of a picture_) instead of a picture_). – Sebastian Simon Sep 05 '18 at 15:22
  • @Xufox this is a chrome exntesion running on a page someone else has written. I'm just inspecting the console and trying to write a simple script that will click a link. – Morgan Allen Sep 05 '18 at 15:34

2 Answers2

2

You can simulate a click by triggering the click event. Something like this:

$(".quickAddItem-0-161").trigger("click");
ZiNNED
  • 2,520
  • 15
  • 24
0

there's many methods, you can check jquery docs examples :

$(divSelector).click();
//
$(divSelector).on('click',callback);

divSelector may be an id or class in your case divSelector is "quickAddItem-0-161"

khofaai
  • 1,255
  • 1
  • 8
  • 19
  • 1
    The first one is correct. The second one sets up a click handler, which isn't what OP is trying to do. – Rup Sep 05 '18 at 15:16