Przemysław Banaszek
Przemysław Banaszek

Reputation: 838

access DOM in polymer

What I want to achieve:

  1. highlight some text in p element

  2. (not add/ or remove) paper-badge when value is 0

  3. change paper-badge color depending on number

Sample module to play with:

<link rel="import" href="https://polygit2.appspot.com/components/polymer/polymer.html">
<link rel="import" href="https://polygit2.appspot.com/components/iron-icons/communication-icons.html">
<link rel="import" href="https://polygit2.appspot.com/components/paper-icon-button/paper-icon-button.html">
<link rel="import" href="https://polygit2.appspot.com/components/paper-badge/paper-badge.html">

<dom-module id="question-one">
    <template>
        <template is="dom-repeat" items="{{numbers}}">
            <p>color one of my letters</p>
            <paper-icon-button id="someid{{index}}" icon="communication:email"></paper-icon-button>
            <paper-badge for="someid{{index}}" label="{{item}}"> </paper-badge>
        </template>
    </template>
    <script>
        Polymer({
            is: "question-one",
            properties: {
                numbers: {
                    type: Array,
                    value: function () {
                        return [0, 1, 2, 0, 4, 5, 0, 7, 8, 0]
                    }
                }
            }
        }); </script>
</dom-module>

and usage:

<!DOCTYPE html>
<html lang="en">
<head>
    <script src="https://polygit2.appspot.com/components/webcomponentsjs/webcomponents-lite.js"></script>
    <link rel="import" href="questions//question-one.html" />
</head>
<body>
    <question-one></question-one>
</body>
</html>

Upvotes: 0

Views: 615

Answers (1)

G&#252;nter Z&#246;chbauer
G&#252;nter Z&#246;chbauer

Reputation: 658067

Add an id attribute

<p id="someId">

use the $ map to get a reference to the element

this.$.someId.innerText...

this.$ doesn't work for dynamically added HTML or HTML inside template is="dom-if" ortemplate is="dom-repeat" or similar.

For such cases you can use this.$$(selector) where all CSS selectors are supported, while this.$... only supports id (without #)

Upvotes: 3

Related Questions