You are viewing a plain text version of this content. The canonical link for it is here.
Posted to batik-users@xmlgraphics.apache.org by mlindeboom <mi...@bms.com> on 2006/05/11 21:10:26 UTC

getElementById question

When I load the following svg file I get an error. It seems that if a group
or other element  is created using code, the getElementById() function
cannot find it later. Is this true or is there something else wrong with the
script? 

<svg  width="500" height="500" onload="main(evt)"> 
<script><![CDATA[ 
function main(evt){ 
  var svgdoc = evt.target.ownerDocument; 
  var root = svgdoc.firstChild; 
  var rootnode = svgdoc.getElementById('root') 
  buildgroups(svgdoc,'xx'); 
  buildLabel(svgdoc,'xx'); 

} 
function buildgroups(mydoc, prefix){ 
  var root = mydoc.getElementById('root'); 
  //p1 
  var node = mydoc.createElement('g'); 
  node.setAttribute('id',prefix + 'p1'); 
  root.appendChild(node); 
} 
function buildLabel(mydoc, prefix){ 
  var xxp1 = mydoc.getElementById(prefix + 'p1'); 
  var node = xxp1.createElement('text'); 
  xxp1.appendChild(node); 
} 
]]></script> 
<g id='root'> </g> 
</svg> 


--
View this message in context: http://www.nabble.com/getElementById-question-t1603253.html#a4346978
Sent from the Batik - Users forum at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org


Re: getElementById question

Posted by th...@kodak.com.
Hi Michael,

mlindeboom <mi...@bms.com> wrote on 05/11/2006 03:10:26 PM:

> When I load the following svg file I get an error. It seems that if a 
group
> or other element  is created using code, the getElementById() function
> cannot find it later. Is this true or is there something else wrong with 
the
> script? 

   There are several things wrong with the file/script..

> <svg  width="500" height="500" onload="main(evt)"> 

   First you really need to declare your namespaces:
<svg  width="500" height="500" onload="main(evt)"
      xmlns="http://www.w3.org/2000/svg"> 

>   var node = mydoc.createElement('g'); 

   Second you almost certainly want:
   var SVGNS = "http://www.w3.org/2000/svg";
   var node = mydoc.createElementNS(SVGNS, 'g');

   Unless you really want to create an element in
the no-name namespace called 'g' that isn't an SVG
'g' element...

>   var node = xxp1.createElement('text'); 

   Third, the 'g' element doesn't have createElement
(or createElementNS) that method is on the SVGDocument:
    var node = mydoc.createElementNS(SVGNS, 'text');

   Good luck!

---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org