Recently, I've been given the task of manually updating a UI that was developed using Next.js. One particular element in the UI is repeated countless times (okay, not exactly a million but you get it), and my job is to toggle its state by clicking on it.
Finding the selector for the element was pretty straightforward. After typing
const el = document.querySelector('some-selector')
in the console, I successfully retrieved the correct element. The only issue arose when I attempted to toggle the element state using el.click()
. Even after trying out el.dispatchEvent(new MouseEvent("click", { bubbles: true, cancelable: false }));
, I couldn't get the desired result.
Take Nike's website as an example, which uses Next.js. If you visit their homepage and inspect the console, you can select the swoosh icon with
const swoosh = document.querySelector('.swoosh')
. My objective is to mimic a click on that element directly from the console.
I suspect that the root of the issue lies in the custom click handler implemented within the next.js component. It seems like this handler doesn't get triggered unless I physically interact with the element. Does anyone have any idea how I can invoke the actual next.js click handler programmatically?