JavaScript Polymer 2.0 Access Element Inside of Nested Template by Id polymer access element in template,polymer access element by id,

Polymer provides access to elements by id via this.$['foo']. However, I find that I am unable to access elements by id that are in nested templates.

<template>
     <vaadin-dialog id="dialog" >
          <template>
     <work-order-new  id="workordernew" on-submitted="_onWorkOrderSubmitted"> </work-order-new>
          </template>
     </vaadin-dialog>
    </template>

In this situation this.$.dialog works but this.$.workordernew does not. Are you able to access elements inside of a nested template by id and if so how?

I also tried both of approches below and they also didn't work .

Polymer.dom(this.root).querySelector():

this.$$(selector)

I saw a lot of answers for this question but none of them solved my problem.

Answer:1

The issue you're facing is not strictly a Polymer issue, but more a reflection of what Vaadin is doing behing the scenes.

When the vaadin-dialog element is created the actual contents of the template are copied in an other element - vaadin-dialog-overlay - rather than staying in the original element.

The overlay element is inserted at the document level, so you can grab it querying for it:

let vaadinOverlay = document.querySelector('vaadin-dialog-overlay');

Once you have the overlay it's easy to get to your work-order-new element:

let workOrder = vaadinOverlay.$.content.querySelector('#workordernew');

The real question would be in this case: why are you trying to access your elements directly?

Usually, when working with components, getting to the point when you need to directly access a component is usually a good indication that there is something broken in the design of the application.

<base target='_blank' href="https://polygit.org/vaadin-dialog+vaadin+v1.0.0-alpha1/vaadin-button+vaadin+v1.0.0-alpha1/polymer+^2.0.0/components/">

<script src="webcomponentsjs/webcomponents-lite.js"></script>

<link rel="import" target='_blank' href="vaadin-dialog/vaadin-dialog.html">
<link rel="import" target='_blank' href="vaadin-button/vaadin-button.html">


<my-element></my-element>

<dom-module id="my-element">
  <template>
    <vaadin-dialog opened>
      <template>
        <work-order-new id="workorder"></work-order-new>
      </template>
    </vaadin-dialog>
  </template>
</dom-module>

<dom-module id="work-order-new">
  <template>
    <h4>Work Order</h4>
    <input type="text" id="inputbox" value="Default Value">
  </template>
</dom-module>

<script>
  // Extend Polymer.Element base class
  class MyElement extends Polymer.Element {
    static get is() {
      return 'my-element';
    }

    constructor() {
      super();
    }

    connectedCallback() {
      super.connectedCallback();

      /**
       * Fetch the reference to the overlay.
       **/
      let vaadinOverlay = document.querySelector('vaadin-dialog-overlay');
      
      /**
       * Fetch the reference to the `work-order-new` element.
       **/
      let workOrder = vaadinOverlay.$.content.querySelector('#workorder');

      /**
       * Wait for the overlay to be setup, this is just to show
       * that we can now use the reference to the `work-order-new`
       * element.
       **/
      setTimeout(() => {
        let inputBox = workOrder.$.inputbox;
        inputBox.value = `Changed from my-element`;
      }, 1500);
    }
  }

  customElements.define(MyElement.is, MyElement);

  class WorkOrderNewElement extends Polymer.Element {
    static get is() {
      return 'work-order-new';
    }

    constructor() {
      super();
    }

    connectedCallback() {
      super.connectedCallback();
    }
  }

  customElements.define(WorkOrderNewElement.is, WorkOrderNewElement);
</script>
Answer:2

I have a text field in an MVC application that displays the date and time. The date is created using a datepicker and the time I add to the text field. An example of the text is as follows: 10/23/...

I have a text field in an MVC application that displays the date and time. The date is created using a datepicker and the time I add to the text field. An example of the text is as follows: 10/23/...

  1. time string conversion
  2. python time string conversion
  3. perl time conversion from string
  4. time_t to string conversion
  5. string to timestamp conversion

I'm trying to style the child component of a styled-component, but it sends the css to the parent instead of the child/ This is my code, export const Card = styled.div` position: relative; ${...

I'm trying to style the child component of a styled-component, but it sends the css to the parent instead of the child/ This is my code, export const Card = styled.div` position: relative; ${...

  1. style child components angular 2
  2. style child components
  3. styled components parent child
  4. angular style child components
  5. vue style child components
  6. react style child components
  7. angular style child components from parent

html <div ng-repeat="item in timesheets">{{item.week}}</div> script .controller('TimesheetMainCtrl', function ($rootScope, $scope, $window) { dpd.timesheetold.get(function (...

html <div ng-repeat="item in timesheets">{{item.week}}</div> script .controller('TimesheetMainCtrl', function ($rootScope, $scope, $window) { dpd.timesheetold.get(function (...

in server.js var connect = require('connect'); var http = require('http'); function doFirst(request, response, next) { console.log('bacon'); next(); } function doSecond(request, response, ...

in server.js var connect = require('connect'); var http = require('http'); function doFirst(request, response, next) { console.log('bacon'); next(); } function doSecond(request, response, ...

  1. cannot read property of undefined
  2. cannot read property 'length' of undefined
  3. cannot read property 'setstate' of undefined
  4. cannot read property 'push' of undefined
  5. cannot read property 'map' of undefined
  6. cannot read property 'addeventlistener' of null
  7. cannot read property 'match' of undefined
  8. cannot read property 'then' of undefined
  9. cannot read property '0' of undefined
  10. cannot read property 'props' of undefined
  11. cannot read property 'nativeelement' of undefined
  12. cannot read property 'tolowercase' of undefined
  13. cannot read property 'state' of undefined
  14. cannot read property 'foreach' of undefined
  15. cannot read property 'split' of undefined
  16. cannot read property of null
  17. cannot read property 'replace' of undefined
  18. cannot read property 'summary' of undefined
  19. cannot read property 'createelement' of undefined
  20. cannot read property 'subscribe' of undefined