Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unable to access dom-if content by id #3170

Closed
Qvatra opened this issue Dec 7, 2015 · 14 comments
Closed

unable to access dom-if content by id #3170

Qvatra opened this issue Dec 7, 2015 · 14 comments
Labels

Comments

@Qvatra
Copy link

Qvatra commented Dec 7, 2015

In a custom element I want to access a span and append a child to it but all usual accessors give undefined:

<template>
    <template is="dom-if" if="[[condition]]" restamp>
        <span id="myspan"></span>
    </template>
</template>

 ready() {
   var a = this.$.myspan;                     //<------- is undefined
   var b = this.$$.myspan;                    //<------- is undefined
   var c = document.getElementById("myspan"); //<------- is undefined
   //UPDATE
   var d = this.$$("#myspan");                //<------- is undefined
}

Interesting that outside ready function (after a bit of time when all components are loaded) I can access it in a console via document.getElementById("myspan")... but I need a place in a code to append a child.

How to access a span in this case?

related: http://stackoverflow.com/questions/34138718/how-to-access-content-of-dom-if-inside-a-custom-element

@TimvdLippe
Copy link
Contributor

https://www.polymer-project.org/1.0/docs/devguide/local-dom.html#node-finding

Note: Nodes created dynamically using data binding (including those in dom-repeat and dom-if templates) are not added to the this.$ hash. The hash includes only statically created local DOM nodes (that is, the nodes defined in the element’s outermost template).

For locating dynamically-created nodes in your element’s local DOM, use the $$ method:
this.$$(selector)
$$ returns the first node in the local DOM that matches selector.

Edit: So what should work is this.$$('#span') or this.$$('span').

@Qvatra
Copy link
Author

Qvatra commented Dec 7, 2015

@JeremybellEU, this.$$("#myspan") is also undefined

@MeTaNoV
Copy link

MeTaNoV commented Dec 7, 2015

is the condition met at ready time?

@Qvatra
Copy link
Author

Qvatra commented Dec 7, 2015

condition is a function:

_eq(a, b) {
    return a === b;
 }

and in the ready function I already have defined ''a'' and ''b'' values.

not sure if this is what you was asking...

@MeTaNoV
Copy link

MeTaNoV commented Dec 7, 2015

did you verify that your <span> is being stamped before trying to access it?

@Qvatra
Copy link
Author

Qvatra commented Dec 7, 2015

emm.. no, how to verify that?

@arthurevans
Copy link

The template helpers (dom-if, dom-repeat, dom-bind) fire a dom-change event when they create or remove nodes.

https://www.polymer-project.org/1.0/docs/devguide/templates.html#dom-change

This is a little confusing (and could be clarified better in the docs), but the dom-if is just another element in your element's local DOM. When ready is called on your custom element, the if value has been set on your dom-if, but it hasn't had a chance to instantiate its DOM yet.

For more discussion on this, see this doc bug:

Polymer/old-docs-site#1456

@Qvatra
Copy link
Author

Qvatra commented Dec 8, 2015

@arthurevans, that is helpfulll. one question though:
does render() method on dom-if element guarantees that dom-if children are ready?

@ebidel
Copy link
Contributor

ebidel commented Dec 8, 2015

DOM changes are async. One way around this is to wait for the dom-change event or wrap your call with this.async() so it waits for the next frame. At that point the DOM will be stamped.

@ebidel ebidel closed this as completed Dec 8, 2015
@ebidel ebidel added the question label Dec 8, 2015
@dtruffaut
Copy link

For those who encounter same difficulties :

this.$$('#cssId')

works for me.

@marko911
Copy link

marko911 commented Nov 13, 2016

DOM changes are async. One way around this is to wait for the dom-change event or wrap your call with this.async() so it waits for the next frame. At that point the DOM will be stamped.
👍 1

@ebidel
I wrapped my call in async without giving it a time and the selector this.$$('myId') still returned null after the if variable turned truthy in dom-if

@the-catalin
Copy link

@marko911 you need to use # when querying by id, like this:
this.$$('#myId')

@ernsheong
Copy link

this.$$ is no longer in Polymer 2.0.

this.shadowRoot.querySelector(...) is the way to go:
https://www.polymer-project.org/2.0/docs/devguide/dom-template#node-finding

@kevinsbk
Copy link

"This.$" works if you call super.ready() before accessing the DOM element in ready().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

10 participants