Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot querySelector in my Polymer component

I'm trying to access to a part of my component by querySelector, but it return null.

My html in my component looks like :

<nav class="mainmenu">
   <div class="container">
       <div class="dropdown" id="my-dropdown">

The reason I want to access this, is because, I want to change classes in certains conditions when user click somewhere in body.

void ready(){
 document.body.onClick.listen((E)  {
   if(_isActive){
    querySelector("#my-dropdown");

the querySelector always retun null. Why ?

I understand shadow dom forgive me to access in other dom component, but why in the same component ? How can I access to it ? Or maybe I should process differently ?

like image 390
Nicolas François Avatar asked Apr 15 '14 00:04

Nicolas François


1 Answers

When you use querySelector("#my-dropdown") you are using the top level function querySelector. This function search in the main document and not inside the ShadowDOMs of the polymer elements.

In your case you should use shadowRoot.querySelector('#my-dropdown') or $['my-dropdown'] (it's a shortcut) to get the element you are querying.

like image 51
Alexandre Ardhuin Avatar answered Oct 18 '22 02:10

Alexandre Ardhuin