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

Problem with getElementById?

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/Problem-with-getElementById--t1600327.html#a4339555
Sent from the Batik - Dev forum at Nabble.com.


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


Re: Problem with getElementById?

Posted by Cameron McCormack <ca...@mcc.id.au>.
Bjőrn Hőhrmann:
> Cameron meant to write
> 
>   node.setAttributeNS(null, 'id', prefix + 'p1');

Sure did.

-- 
 Cameron McCormack			ICQ: 26955922
 cam (at) mcc.id.au			MSN: cam (at) mcc.id.au
 http://mcc.id.au/			JBR: heycam (at) jabber.org

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


Re: Problem with getElementById?

Posted by Bjoern Hoehrmann <de...@gmx.net>.
* Cameron McCormack wrote:
>>   node.setAttribute('id',prefix + 'p1');
>
>Should be:
>
>  node.setAttributeNS('id', prefix + 'p1');
>
>but in Batik this will be OK, perhaps not in other UAs.

Cameron meant to write

  node.setAttributeNS(null, 'id', prefix + 'p1');
-- 
Björn Höhrmann · mailto:bjoern@hoehrmann.de · http://bjoern.hoehrmann.de
Weinh. Str. 22 · Telefon: +49(0)621/4309674 · http://www.bjoernsworld.de
68309 Mannheim · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/ 

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


Re: Problem with getElementById?

Posted by Cameron McCormack <ca...@mcc.id.au>.
Hi.

mlindeboom:
> 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 other problems with the document, which may or may not be
related:

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

This should have the SVG namespace declaration:

  <svg xmlns=”http://www.w3.org/2000/svg” …>

> <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');

  var node = mydoc.createElementNS("http://www.w3.org/2000/svg", "g");

>   node.setAttribute('id',prefix + 'p1');

Should be:

  node.setAttributeNS('id', prefix + 'p1');

but in Batik this will be OK, perhaps not in other UAs.

>   root.appendChild(node);
> }
> function buildLabel(mydoc, prefix){
>   var xxp1 = mydoc.getElementById(prefix + 'p1');
>   var node = xxp1.createElement('text');

  var node = xxp1.createElementNS("http://www.w3.org/2000/svg", "text");

>   xxp1.appendChild(node);	
> }
> ]]></script>
> <g id='root'> </g>
> </svg>

With these changes do you still have the problem?

-- 
 Cameron McCormack			ICQ: 26955922
 cam (at) mcc.id.au			MSN: cam (at) mcc.id.au
 http://mcc.id.au/			JBR: heycam (at) jabber.org

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