4

I'm making an app with Lightning Components, and i'm using the navigateToComponent event. It's working fine, allows me to navigate from one component to another, problem is: - When i navigate to another component, dynamic javascript is not working anymore. Example:

    document.getElementById("options").style.display = 'block';
    document.getElementById("open-icon").style.display = 'none';

This works fine when i open the open, but as soon as i go to another component is not working anymore. I inspect with chrome, the controller is being called, the divs are there, and the controller is detecting them, but simple doesn't change nothing.

Anyone as a suggestion?

EDIT: This is happening with all javascript dynamic functions, doesn't hide, or show any div...

  • 3
    Using DOM Id values means your component can break if combined with another component that is using the same Id values which is bad. The Locker Service also intrudes if you are using multiple namespaces. (Might be the cause of your problem.) Hence the aura:id mechanism as described in document.getElementById(“something”).value not working for <Select> with <option> not working after Locker Service enabled. – Keith C Apr 17 '17 at 11:05
  • Since i'm in a different component, i thought i could just use the same id's, because the previous ones were not in the DOM (supposedly) anymore. It's working now, i just changed the id's. One more question: When using navigateToComponent do i need to load styles and scripts in all individual components? I tried to wrap all components in a single one and load the external files there, but takes no effect. – Ricardo Coutinho Apr 17 '17 at 11:16
  • The point about a normal DOM Id is that the values need to be unique across the whole page. So your logic will break if there is more than one instance of your component or if your component is mixed in with another component that uses the same Id values. So coding using DOM Id values is an anti-pattern. – Keith C Apr 17 '17 at 12:09
  • On the styles and scripts, my assumption is that its cleaner for a component to be self-contained but there are patterns for sharing code - see e.g. Modularizing Code in Lightning Components. – Keith C Apr 17 '17 at 12:09

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.