0

I have the following HTML in my LWC component-

<template>
<div class="firstdiv">
    <div class="firstinsidefirstdiv fifd">
        1234
    </div>
    <div class="secondinsidefirstdiv sifd">
        5678
    </div>
</div>
<div class="seconddiv">
    <!-- <div data-id="firstinsideseconddiv fisd"> -->
    <div class="firstinsideseconddiv fisd">
        9876
    </div>
    <div class="secondinsideseconddiv sisd">
        5432
    </div>
</div>
<lightning-record-view-form
            record-id="00xxxxxxxxxxxJ"
            object-api-name="Contact">
    <div class="slds-box slds-theme_default">
        <span>inside span</span>
        <lightning-output-field field-name="Name">
        </lightning-output-field>
        <lightning-output-field field-name="Phone">
        </lightning-output-field>
        <lightning-output-field field-name="Email">
        </lightning-output-field>
        <lightning-output-field field-name="Birthdate">
        </lightning-output-field>
        <lightning-output-field field-name="LeadSource">
        </lightning-output-field>
    </div>
</lightning-record-view-form>

and the following JS -

renderedCallback(){
    let div = this.template.querySelectorAll('.seconddiv>.firstinsideseconddiv');
    console.log("inside renderedCallback");
    div.forEach(element => {
       console.log("text content " + element.textContent);
       console.log("inner html " + element.innerHTML);
    });

    console.log('second console');
    div = this.template.querySelectorAll('lightning-output-field');
    console.log(this.template);
    console.log(this.template.childNodes);
    console.log(this.template.childNodes[2].children["0"].children[1].outerHTML);
    console.log(this.template.childNodes[2].children["0"].children[1].shadowRoot);
    console.log(this.template.childNodes[2].children["0"].children[1].shadowRoot.children[0].innerHTML);

I am unable to access the shadowRoot element even though it shows up in the dom in chrome- enter image description here Also when i try to print the innerText of "" nothing is printed even though it has a value- enter image description here

Please let me know how I can access the shadowRoot of an element and why the innerText is not being printed.

4
  • You forget to add JS code and the please explain your problem too – Sahil Malhotra Feb 8 '20 at 10:07
  • I have updated the question, my net was slow so somehow only half the question got published. – user2246808 Feb 8 '20 at 10:14
  • 1
    This question is a Y in an X-Y Problem situation. Yes, you can see the "innerText" of components in the developer console, because everything can be seen there. However, Aura/LWC/Lightning has a concept of the Locker API, which, among other things, prevents code in package "A" from accessing package "B" except in narrowly defined channels. This is a security feature, and it limits your script's access to other scripts. All that said, what is your "X"? What are you trying to achieve? – sfdcfox Feb 8 '20 at 15:42
  • @sfdcfox I am trying to access the div and span elements which are created by salesforce when the tag <lightning-output-field field-name="Name"> is rendered. Also I am trying to see if I can access the child elements tags from the parent component. And trying to see how can the shadoowRoot elements be accessed in JS. – user2246808 Feb 9 '20 at 12:46
1

You can't access information you don't have access to. This is a security feature, and a core principle of Web Components in general (allowing components to protect their contents).

I wrote a Playground example that demonstrates this. As I stated in my comment, you won't be able to access the shadow root of the the lightning namespace components and its contents. You can only access information that is not already hidden from your script.

Whatever you're attempting to do (your "X"), this is an X-Y Problem. You will need to find a different solution ("Y") to solve your "X" problem.

1
  • 1
    Thanks SFDCfox , there is no X problem , I just wanted to see whether I could or not. Thanks for your time, keep rocking. – user2246808 Feb 10 '20 at 4:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.