skip to Main Content

open tag audio id="song"
source id="file-path" src="media/Kaattumalli.mp3" type="audio/mp3"
audio close tag

I tried many ways to find the solution. But, I failed to take src value into a javascript variable. Anyone Know the trick to solve the problem. Please let me know the trick.
How to take the src file name and store the value in a variable using javascript. Please any help me.

3

Answers


  1. You need to use id of the audio tag to get the element and then you can get attributes from the tag using that element

    const audio = document.getElementById("file-path")
    const audioSrc = audio.src;
    console.log(audioSrc);
    
    Login or Signup to reply.
  2. To retrieve the value of the src attribute from an HTML , tag and store it in a JavaScript variable, than you can use the getAttribute() method.

    Here’s an example of how you can achieve this for your case:

    const audioElement = document.getElementById('song');
    
    // Get the src attribute value
    const srcValue = audioElement.getAttribute('src');
    Login or Signup to reply.
  3. <script>
       var a = document.getElementById("song");
       var source = a.getElementsByTagName('source')[0];
       var src = source.getAttribute('src');
       alert('src');
    </script>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search