title: Loading Audio Element After Dynamically Changing the Source
body: <p>I have a couple of audio elements that appear in the body of my page.  They look like this.</p>
  
  <pre><code>      &lt;audio id="sound1" preload="auto"&gt;
        &lt;source id="sound1source" src="../../Content/Audio/gau.mp3"&gt;
        //add .ogg here later
        &lt;/audio&gt;
        &lt;audio id="sound2" preload="auto"&gt;
        &lt;source id="sound2source" src="../../Content/Audio/mah.mp3"&gt;
        //add .ogg here later
        &lt;/audio&gt;
  </code></pre>
  
  <p>The audio plays when a user mouses over certain divs.  Here's the code that triggers it.</p>
  
  <pre><code>var audio = $("#sound1")[0];
  $("#ChoiceA").mouseenter(function () {
      audio.play();
  });
  var audio2 = $("#sound2")[0];
  $("#ChoiceB").mouseenter(function () {
      audio2.play();
  });
  </code></pre>
  
  <p>Everything above works fine.  My problem occurs when I attempt to dynamically change the source element after making an ajax call.  Here's my javascript that accomplishes that.</p>
  
  <pre><code>var src1 = "../../Content/Audio/" + data.nouns[0].Audio1 + ".mp3";
  var src2 = "../../Content/Audio/" + data.nouns[1].Audio1 + ".mp3";
  
  $("#sound1source").attr("src", src1);
  $("#sound2source").attr("src", src2);
  </code></pre>
  
  <p>When I inspect the page after triggering the ajax call to change the source path for the audio elements, I see that the source <em>is</em> updated.  No problem there.  The problem is that the audio that the new paths point to does not play.</p>
  
  <p>After hunting around I found this note on <a href="http://www.w3.org/TR/html5/video.html" rel="noreferrer">w3.org</a> <em>"Dynamically modifying a source element and its attribute when the element is already inserted in a video or audio element will have no effect. To change what is playing, either just use the src attribute on the media element directly, or call the load() method on the media element after manipulating the source elements."</em></p>
  
  <p>The comment on w3.org seems to be related so I tried calling $('#sound1').load() and also $('#sound1source').load().  Neither solved my problem.</p>
  
  <p>Can someone tell me what I've done wrong? If I need to cause the audio element to load again after dynamically changing the src, how do I do that?</p>
  
  <p>-------------UPDATE-------------</p>
  
  <p>Based on Swatkins suggestion I created the following function to create the audio tag when the user mouses over the target div.  Unfortunately this has not solved the problem either.</p>
  
  <pre><code>    function attachAudio1(src) {
  
          $('#audio1').remove();
          var audio = $('&lt;audio&gt;');
          audio.attr("src", src);
          audio.attr("id", "audio1");
          audio.appendTo('body');
          attachPlayAction();
      };
  
      function attachPlayAction() {
          var audio = $("#audio1")[0];
          $('#ChoiceA').live('mouseenter', function () {
              audio.play();
          });
  
  
      };
  </code></pre>