Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

115
Views
I want to change audio source onClick

I want to change audio source onClick button. I am trying this but I can't take a result. enter image description here

data() {
    return {
      audioSrc: ''
    }
  },
  methods: {
    setActiveAudio(item) {
      this.$refs.audioElm.src = item;
      this.$refs.audioElm.currentTime = 0;
      this.$refs.audioElm.crossOrigin = 'anonymus';
      this.$refs.audioElm.play();
    },
  },
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<span class="voice-btn" @click="setActiveAudio('../assets/voice.mp3')"> Click Sound</span>
<audio ref="audioElm" :src="audioSrc"></audio>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You have to use a timer to make sure the DOM has been updated before you interact with the element like

    data() {
    return {
      audioSrc: ''
    }
  },
  methods: {
    setActiveAudio(item) {
      this.audioSrc = item;

      var element = this.$refs.audioElm;
      setTimeout(function()
      {
        element.currentTime = 0;
        element.crossOrigin = 'anonymus';
        element.play();      
      }, 0);
    },
  }

Working example on https://jsfiddle.net/AndersBillLinden/6x8hzrga/

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!