Salesforce Asked by Jason Clark on January 4, 2022
In an LWC component, this.template.querySelector()
appears to return an Element (or probably SecureElement) for standard HTML elements, but something else for child components. For example:
this.template.querySelector('div').scrollIntoView();
This will find the first div and correctly scroll it into view. On the other hand:
this.template.querySelector('c-child').scrollIntoView();
does not work. I can verify that this.template.querySelector('c-child')
is indeed returning an object that represents the first c-child
custom LWC component, and the call to scrollIntoView()
does not return an error, but the display does not move. I assume that the object return is not the SecureElement representing the <c-child>
node in my <template>
.
I am able to get the desired behavior by adding a public api method to c-child
:
@api
scrollToHere() {
this.template.querySelector('div').scrollIntoView();
}
and calling it from the parent, e.g.,
this.template.querySelector('c-child').scrollToHere();
But this seems unnecessary since there should be a element in the DOM to represent the <c-child>
element, but either .querySelector('c-child')
doesn’t return that element, or LWC elements don’t support standard SecureElement methods such as .scrollIntoView()
. I tried a few variations such as this.template.querySelector('c-child').element.scrollIntoView();
without success.
2 Asked on November 25, 2021
2 Asked on November 25, 2021 by tekill
2 Asked on November 25, 2021
1 Asked on November 25, 2021 by test-adg10
0 Asked on November 25, 2021 by sai-venkat-prakash
1 Asked on November 25, 2021 by ceez
1 Asked on November 25, 2021 by sfdc-dev-27
1 Asked on November 25, 2021 by j-schreiber
1 Asked on November 23, 2021 by vinay-chanchlani
2 Asked on November 23, 2021 by tom-condon
4 Asked on November 23, 2021
1 Asked on November 23, 2021 by laureant
1 Asked on November 23, 2021
dashboard filters leads lightning experience territory management
1 Asked on November 23, 2021 by jegoxab
0 Asked on November 23, 2021 by user40950
1 Asked on November 23, 2021
Get help from others!
Recent Questions
Recent Answers
© 2023 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir