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.
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.
1 Answer
Reset to default 16$
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();
$
), but that's it. You'd need to implement it yourself when interacting within the page as @Li357 said. – Phix Commented Dec 14, 2017 at 3:04$('#app .mylink').click()
is firing click already on an element? – Casa Lim Commented Dec 14, 2017 at 3:14