#Uncaught TypeError: nextElement.click is not a function

13 messages · Page 1 of 1 (latest)

sly kiln
#

I'm trying to click a HTMLElement using click() method but i got that error message. in the down below section I just added my function that will click a element.

export const clickNextSibling = (
  clickedElement: SVGCircleElement,
  max: number = 6
) => {
  let nextElement: HTMLElement =
    clickedElement.nextElementSibling as HTMLElement;
  if (nextElement) {
    nextElement.click();
    console.log("clicked!");
  }
};
#

Uncaught TypeError: nextElement.click is not a function

tiny cradle
#

if clickedElement is an SVGElement, why would its nextElementSibling be an HTMLElement?

assertions are unsafe, you're only supposed to use them when there's some info that ts doesn't have, but you do. this is info that you don't have either

sly kiln
#

see the clicked element and the next element

tiny cradle
#

yeah, it's an SVGElement, not an HTMLElement.

#

click is not a method defined on SVGElements, apparently

sly kiln
#

so, how can I click the next element using ts??

tiny cradle
#

google is a great tool, consider using it.

sly kiln
#

thank you brother let me try with this

sly kiln
#

@tiny cradle

just one question, when initial or first click is dispatched then I want to wait 5 second after that I want to run that loop like next sibling next sibling...

// click nextsibling function
export const clickNextSibling = async (
  clickedElement: SVGAElement,
  max: number = 6
) => {
  await delay(5);
  dispatchClick(clickedElement);
  let nextElementSibling: Element =
    clickedElement.nextElementSibling as Element;
  let count = 1;
  while (nextElementSibling && count <= max) {
    dispatchClick(nextElementSibling);
    nextElementSibling = nextElementSibling.nextElementSibling as Element;
    count++;
  }
};


// and this is my delay funtion
export const delay = (ms: number): Promise<unknown> => {
  return new Promise((resolve) => setTimeout(resolve, ms * 1000));
};
tiny cradle
#

well, your logic isn't really following that

#

you're waiting before you "clickNextSibling", but you never "clickNextSibling" again