javascript - Polymer 1.0 can't access elements within nested <template> element -
i using polymer 1.0 , building small accordion example. have data binding accordion text fine, want change icon of accordion when click it.
below code
<dom-module id="ab-accordion"> <template> <iron-ajax auto handle-as="json" on-response="handleresponse" debounce-duration="300" id="ajaxreq"></iron-ajax> <template is="dom-repeat" id="accordion" items="{{items}}" as="item"> <div class="accordion" on-click="toggleparentacc"> <div id="accordion_header" class="accordion__header is-collapsed"> <i class="icon icon--chevron-down"></i> <span>{{item.name}}</span> </div> <div id="standard_accordion_body" class="accordion__body can-collapse"> <div class="accordion__content"> <content id="content"></content> </div> </div> </div> </template> </template> <script> polymer({ is: "ab-accordion", //properties element properties: { accordian: object, childaccordions: object, // param passed in element - set if accordion open default. open: string, data: string, requrl: { type: string, value: "https://example.com/service.aspx" }, }, ready: function () { this.items = []; }, attached: function () { // run once element attached dom. }, toggleparentacc: function (event) { // toggle classes of accordions //this want toggle class this.$.accordion_header.classlist.toggle('is-collapsed'); if (typeof event !== 'undefined') { event.stoppropagation(); // stop click going parent. } }, handleresponse: function (e) { this.items = e.detail.response.sports; } }); </script> </dom-module>
basically inside toggleparentacc
function want toggle class of div id accordion_header
. undefined or null.
i have tried following 2 lines:
this.$.accordion_header // #1 this.$$('#accordion_header') // #2
how access element inside dom-repeat?
update: can't access elements within when inside attached function.
attached: function(){ this.$.accordion_header // null?! this.$$('#accordion_header'); // null! }
https://www.polymer-project.org/1.0/docs/devguide/local-dom.html#node-finding
note: nodes created dynamically using data binding (including in dom-repeat , dom-if templates) not added this.$ hash. hash includes statically created local dom nodes (that is, nodes defined in element’s outermost template).
i think better if you'd use polymer.dom(this.root)
instead. i'd advice not use static ids in dom-repeat
meant unique. use classes instead.
Comments
Post a Comment