title: Inline SVG and HTML DOM Scope for .getElementById
body: <h2>The problem space:</h2>
  
  <p>I have an SVG map that I embed inline to html using JQUERY Ajax.  Then I use java script to address many elements in the svg object to change attributes and add event listeners based on data series.  The data and the maps are interactive.  Users can change state over many permutations. </p>
  
  <p>I am also using JQUERY UI 1.10.3 Tabs</p>
  
  <p>There are numerous categories.  Therefore depending on the presentation there could be multiple copies of the svg map on the same Tab and many different Tabs having the same svg map.</p>
  
  <p>Each copy of the svg document has the same element IDs. The problem I am experiencing is that the element IDs are conflicting with each other in the HTML DOM.  So that the only elements that are addressable are the elements from the svg object that occurs first in the HTML document.</p>
  
  <h2>My question is:</h2>
  
  <p>Do I need to make the elements IDs unique for each instance of the svg object?  Or is there a way to set scope and context to the svg object in the HTML DOM?</p>
  
  <h2>Example:</h2>
  
  <p>Each containing div has a unique ID that I use to insert the svg document and retrieve the SVG document.</p>
  
  <pre><code>    &lt;div id="svgMap_div_1" class="cIG" style="height: 500px; width: 750px"&gt;&lt;/div&gt;
  </code></pre>
  
  <p>Code used to load the svg document that is received from ajax (mimeType = "image/svg+xml"):</p>
  
  <pre><code>    document.getElementById('svgMap_div_1').appendChild(svgXMLObject.documentElement)
  </code></pre>
  
  <p>Code used to retrieve the svg document.  The declaration for svgDoc is local to the function it is implemented in and is unique.</p>
  
  <pre><code>    var svgDoc = document.getElementById('svgMap_div_1').ownerDocument;
  </code></pre>
  
  <p>Code used to update the individual elements.  When the identical svg objects (child elements have the same IDs) are in the same HTML DOM, then this javascript will always address the svg object that occurs first in the HTML DOM even if it is retrieved from a discrete div  for example id='svgMap_div_2'.</p>
  
  <pre><code>    var svgElem = svgDoc.getElementById('elemID');
      svgElem.setAttribute("fill", '#0000FF');
  </code></pre>
  
  <p>I believe that the root of the problem is that svgDoc is retrieved using ownerDocument.  Rather than create a discrete object with scope and context, it reaches through to the entire DOM so that the DOM is the context.  See the code reference from the IDE:</p>
  
  <pre><code>HTMLDocument Node.ownerDocument
  
      Property ownerDocument http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/ecma-script-binding.html 
      See Also:
      Document
      Since:
      Standard ECMA-262 3rd. Edition
      Level 2 Document Object Model Core Definition.
      @type
      Document
  </code></pre>
  
  <p>I can develop a working algorithm to make all of the ids unique if I have to but I sure would like to find a better way. I have experimented with other methods and properties to get a working svg document object but none of these are working for me. </p>
  
  <p>Below is a working example to demonstrate what happens when both svg objects are in different div on the same page.  All four combinations change the color of the first rectangle.  If you change the ID for the rect in the second object to 'box-b' and address it accordingly ('bbox_div', 'box-b'), then both rectangles are operated on independent from one another.</p>
  
  <pre><code>&lt;script type='text/javascript'&gt;
  function colorBox(divID, svgElem, colorHex) {
      /* DOM  This is the original solution that did not work !!!*/
      //    var svgDoc = document.getElementById(divID).ownerDocument.
      //    var svgBox = svgDoc.getElementById(svgElem);
      //    svgBox.setAttribute("fill", colorHex);
  
      /* querySelector */
      //    document.querySelector("#"+divID + " ."+svgElem).setAttribute("fill", colorHex);
  
      /* jquery div */
      //    var svgBox = $('#'+divID+' svg').find('#'+svgElem);
      //    svgBox.css("fill", colorHex);    
  
      /* jquery class */
      $('#'+divID+' svg').find('.'+svgElem).css("fill", colorHex);
  }
  &lt;/script&gt;
  &lt;div style="height: 100px; width: 100px"&gt;
      &lt;a href="#" onclick="colorBox('abox_div', 'box-a', '#FF0000');return false;"&gt;Box A Red&lt;/a&gt;&lt;br&gt;
      &lt;a href="#" onclick="colorBox('abox_div', 'box-a', '#0000FF');return false;"&gt;Box A Blue&lt;/a&gt;&lt;br&gt;
      &lt;a href="#" onclick="colorBox('bbox_div', 'box-a', '#FF0000');return false;"&gt;Box B Red&lt;/a&gt;&lt;br&gt;
      &lt;a href="#" onclick="colorBox('bbox_div', 'box-a', '#0000FF');return false;"&gt;Box B Blue&lt;/a&gt;&lt;br --&gt;
  &lt;/div&gt;
  &lt;div id="abox_div" style="height: 100px; width: 100px"&gt;
      &lt;svg xmlns="http://www.w3.org/2000/svg"
           width="0.990919in"
           height="0.597218in" 
           viewBox="0 0 71.3461 42.9997"&gt;
  
      &lt;style type="text/css"&gt;
      &lt;![CDATA[
          .allboxes {fill:#00FF00}
      ]]&gt;
      &lt;/style&gt;
  
      &lt;g class="box-a allboxes" transform="translate(0.24,-0.24)"  fill="#e8eef7"&gt;
          &lt;rect x="0" y="0.48" width="70.8661" height="42.5197" &gt;&lt;title&gt;Box A&lt;/title&gt;&lt;/rect&gt;
      &lt;/g&gt;
     &lt;/svg&gt;
  &lt;/div&gt;
  &lt;div id="bbox_div" style="height: 100px; width: 100px"&gt;
     &lt;svg xmlns="http://www.w3.org/2000/svg"
          width="0.990919in"
          height="0.597218in" 
          viewBox="0 0 71.3461 42.9997"&gt;
      &lt;g class="box-a allboxes" transform="translate(0.24,-0.24)"  fill="#e8eef7"&gt;
          &lt;rect x="0" y="0.48" width="70.8661" height="42.5197" &gt;&lt;title&gt;Box B&lt;/title&gt;&lt;/rect&gt;
      &lt;/g&gt;
     &lt;/svg&gt;
  &lt;/div&gt;
  </code></pre>