Casa Lim
Casa Lim

Reputation: 375

Simulate user click in chrome console

Why can't I trigger or simulate user click using chrome's console in browser? like I have a link on a page I do $('#app .mylink').click() it should go somewhere.

Upvotes: 12

Views: 37792

Answers (1)

AuxTaco
AuxTaco

Reputation: 5171

$ in Chrome's console is an alias for document.querySelector(), except when it's not. If $ is declared in the page, usually by jQuery, $ in the console will point to that instead.

Calling click on a jQuery object representing an a element won't perform the native navigation, but calling click on the native HTMLElement will. If you know the page you're working with uses jQuery, you'll need to retrieve the native HTMLElement from the jQuery object:

$('#app .mylink')[0].click(); // assuming you want to click the first element returned

But if jQuery's not involved, that won't work. Best to be unambiguous:

document.querySelector('#app .mylink').click();

Upvotes: 16

Related Questions