Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

When does an element become available in dom after using vue v-if

When does an element become available in the dom after using a vue v-if? I would have thought you could do a query selector on the element after the v-if evaluates to true?

In the below code I need to get the .test element once the popout is shown but it shows as null – how do I get it?

new Vue({
  el: "#app",
  data() {
    return {
      showPopout: false,
    };
  },
  methods: {
    buttonClick(day) {
      this.showPopout = true;
      console.log(document.querySelector('.test'));
    },
  },
});
<script src="https://unpkg.com/vue@2.6.14/dist/vue.js"></script>
<div id='app'>
  <span @click="buttonClick">show popout</span>
  <div v-if="showPopout"><span class="test">test</span></div>
</div>

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

It will be there after nextTick

new Vue({
  el: "#app",
  data() {
    return {
      showPopout: false,
    };
  },
  methods: {
    buttonClick(day) {
      this.showPopout = true;
      this.$nextTick(() => {
        console.log(document.querySelector('.test'));
      });
    },
  },
});
<script src="https://unpkg.com/vue@2.6.14/dist/vue.js"></script>
<div id='app'>
  <span @click="buttonClick">show popout</span>
  <div v-if="showPopout"><span class="test">test</span></div>
</div>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading