2

I'm having a couple of very different components and often I need to do some work on the components root DOM-node. Some of the compos have Version<=39.0 other have 40.0+ and therefore I'm not looking for anything which is relaying on Locker Service Secure Window.

I know already it works fine like this

<div aura:id="thisIsTheRoot">...</div> 

and

var rootElement = component.find("thisIsTheRoot").getElement();

But I don't like the wrapper. Can't we do it easier without wrapper and just pure Javascript? Isn't there a framework-function for this already in place? Wouldn't it be nice to have something like

component.getDOMRootElement();

?

This would help me, because I'm writing JS residing in a static resource which should work across all of my components regardless of any markup-wrapper.

  • Maybe try getGlobalId()? – JRiffe Aug 22 '17 at 15:28
  • I'm pretty sure that there's no way to get at the root element directly. The closest you could probably get to would be to get component.get('v.body'), which will return all of the components in the component. – sfdcfox Aug 22 '17 at 16:16
  • @sfdcfox actually JRiffe's comment lead me to this jquery construct which provides a kind of root element $('[data-aura-rendered-by="'+cmp.getGlobalId()+'"]').get(0) (sorry I'm to bad to write it in vanilla JS but it means "give me the element which has data-aura-redered-by equal to getGlobalId()"). For v<=39 it works great!! Not tested for v40+ and maybe the Locker Service is cutting access just one single level below... :-( – Uwe Heim Aug 22 '17 at 16:26
  • 1
    @UweHeim the vanilla version would just be document.querySelector('[data-aura-rendered-by="'+ cmp.getGlobalId()+'"]'). I would expect Locker Service to not be okay with this request because of the Shadow DOM that it uses. – sfdcfox Aug 22 '17 at 16:27
  • 2
    You can use component.getElement() which returns the root element of your component. – Praveen Aug 23 '17 at 2:13

Your Answer

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

Browse other questions tagged or ask your own question.