Access dom element from in experience builder

domhtmljavascriptlightning-experienceshadow-dom

I am trying to access a dom element from the in lightning experience by adding the script

<script type="text/javascript">
   document.addEventListener('DOMContentLoaded', ()=> {
   let el = document.getElementById('onetrust-consent-sdk')
   console.log('el: ', el)  
   })
</script>

Requirement is to access this element and to change the style property on it but console log is showing null.

Any ideas how I could do this?

Thanks

Best Answer

I know this is a bit late but I just did something similar and I got it to work. I ended up using the window onload event and it worked. Its possible the console is showing null because the body is not rendered / loaded yet.

What I did was add something like the following to the head markup:

<script type="text/javascript"> 
   window.onload = function() {
       var el = document.querySelector('#onetrust-consent-sdk');
       console.log('el:' , el);
       //...
       //el.style.color = red;
   }
</script>

When I did something similar for my scenario it worked and successfully selected from and manipulated the DOM.

Related Topic