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

How to display dynamic variable in HTML

I’m wondering how to display a number in HTML that was created dynamically later on in the code. For instance I initialize the value reportDataLength in data(), and I tested that it was returning the right value by doing a console.log(), but now I want to display this value in HTML?

  name: 'notification-tray',
  data() {
    return {
      headers: [
        {
          text: 'Notifications',
          value: 'Body',
          width: '380px',
        },
      ],
      reportData: [],
      reportDataLength: 0,
    };
  },
  async created() {
    await this.getReportDataLength();
  },
  methods: {
    async getReportDataLength() {
      ... this.reportDataLength = this.reportData.length;
      
      console.log(this.reportDataLength);
    },
  },
};

But obviously when I do something like this,

      <span class="d-none">Notification Button</span>
      <span class="badge">this.reportDataLength</span>

it doesn’t work correctly. The other solutions I saw for similar problems used JQuery, and I feel like there’s a simple way to reference this, but I can’t seem to find it out.

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 :

okay so if you are using pure Javascript , u can use this in your javascript function :

Document.getElementsByClassName('badge').innerHtml=this.reportDataLength ; 

otherwise if you are using vue js you can try something like this in your html file :

      <span class="badge">{{reportDataLength}}</span>
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