title: Getting the id of an asynchronously loaded svg element with Jquery
body: <p>After several hours of fruitless experimentation, web searches, consultation of the Jquery API docs and poring over seemingly related stackoverflow posts, I've decided my only hope to getting this to work is to turn to the community here. Hoping someone can give me some insight into why this isn't working and how to fix it!</p>
  
  <p>Within the web app I'm developing, I use several interactive SVG images, sometimes with multiple instances on a single page. In order to prevent distinct SVG images from stepping on each others' toes, interactive elements within each all carry a "barcode" of sorts, appended to their id attribute. In turn, the barcode for each image is encoded in the id attribute for the svg tag. In order for the page to display the svg with the correct initial state, I need to get the id attribute from the svg immediately after loading and pass it to another script to set the correct attributes within the target SVG.</p>
  
  <p>Individual SVG's are loaded via an AJAX call, thusly:</p>
  
  <pre><code>$(document).ready(function(){
      //mapId is supplied in various ways in different contexts, for example:
      var mapId = $("#mapId").name;
      loadSvgMap(mapId);
  }
  
  function loadSvgMap(mapId) {
  
    return jQuery.ajax({
      url: "[% c.uri_for('/maps/update_map/') %]" + mapId,
      success: function(result) {
          if(result.isOk == false) {
              alert("Bad response from server: " + result.message);
          } else {
              $("#som_map").load(result);
          }
      },
      error: function(result, status, errorThrown) {
          alert("Map retrieval failed: " + status + " " + result.status);
      },
      async: true,
      dataType: 'text',
      cache: false
    });
  }
  </code></pre>
  
  <p>At this stage, I need to get the id attribute of the SVG that was loaded. The problem is, the SVG does not seem to exist on the page when I try to do so:</p>
  
  <pre><code>$(window).load(function() {
      var svg = $("svg")[0];
      console.log(svg);
      //returns "undefined"
  }
  </code></pre>
  
  <p>The issue seems to be that the ajax call has not completed when the next block of code executes, so the SVG is not yet present in the DOM. I've tried using $.when within $(document).ready), and this is the method I would like to use, but it still doesn't seem to wait until the SVG is loaded:</p>
  
  <pre><code>$.when(loadSvgMap(mapId)).done(function(a) {
      var map = $("svg")[0];
      console.log(map);
      //undefined
  });
  </code></pre>
  
  <p>I have figured out a workaround, but it is not ideal because the request fires after every ajax request, not just those that change the SVG...</p>
  
  <pre><code>$(document).ajaxStop(function() {
      var svg = $("svg")[0];
      if (svg != null) {
          var map = svg.id;
          console.log(map);
          // do other stuff
      }
  });
  </code></pre>
  
  <p>Having this fire after every ajax request does not break anything at the moment but if I can get the $.when method working properly, it seems that should be less likely to break things down the road. Any ideas?</p>