Welcome to WebmasterWorld Guest from 34.231.247.139

Forum Moderators: open

Message Too Old, No Replies

XML javascript functions

     
4:48 pm on Sep 12, 2003 (gmt 0)

New User

10+ Year Member

joined:Aug 26, 2003
posts:20
votes: 0


elementName.write() function does not work in XML. I know how to use the method getElementById(""). I know how to get and set attribute by using the method getAttribute() and setAttribute(). What I cannot figure out how to set value in the contents of the element.

Example:


<?xml version="1.0" encoding="iso-8859-1"?>

<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
>
<head>
<script language="JavaScript" type="text/javascript"> <![CDATA[

function load() {
//var frm = document.getElementById('svg_form');
frm.test.value = "The old radius is 100";

}

function circle_click() {
var fChild = document.getElementById('tr1');
document.getElementById("tr1").setData("Hello This is Hitesh");
fChild.setData("Hello This is Hitesh");

var circle = document.getElementById('c1');
var currentRadius = circle.getAttribute("r");

if (currentRadius == 100)
circle.setAttribute("r", currentRadius*2);
else
circle.setAttribute("r", currentRadius*0.5);

var n1 = circle.getAttribute("r")-0;
frm.test.value = "The new radius is " + n1 + " in html";

}
]]>
</script>
</head>

<body id="map" onload="load();">
<form id="svg_form">
<script> var frm = document.getElementById('svg_form');</script>
<table>
<tr id="tr1"></tr>
<tr>
<button onclick ="circle_click();">Change Radius</button>
</tr>
<tr>
<svg:svg id="svg1" width="400" height="400">
<svg:circle id="c1" cx="200" cy="200" r="100"/>
</svg:svg>
</tr>
<tr><td>
<input disabled="yes" size="30" style="border:0" type="text" name="test" value="Not loaded yet" readonly="no"/>
</td></tr>
</table>
</form>
</body>
</html>

If you look at the function circle_clk, you can see I tried to write something in the element "tr1" trying something like setData but no such action takes place because no such function exist. Which method will accomplish the same action as the function write() accomplish with HTML?

Can you tell me which methods work with XML as obviously the write() method doesn't work with XML?

Furthermore, can anyone refer me to a specification document listing all javascript methods that will work with XML?

12:11 am on Sept 14, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member tedster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:May 26, 2000
posts:37301
votes: 0


No one seems to be rising to the occasion so far, Hitesh. I don't play in this territory myself, but I did a bit of searching. Mostly I just found similar questions!

Hope someone comes along with at least an idea or a reference for you, even if they don't have a full solution.

3:54 pm on Sept 14, 2003 (gmt 0)

New User

10+ Year Member

joined:Aug 26, 2003
posts:20
votes: 0


After some research, I found the solution. If you want to do a similar function of document.write() which does not work in XML, you can use the document.getElementById() and then get the firstChild and then set the nodeValue to the contents that you wish to write in. For example,

document.getElementById('tr1').firstChild.nodeValue="Hello this is Hitesh".

It worked for me. Let me know if you have any problems. You can use the above method for setData() too. If you are wondering how I came to this solution, pls go to this XML DOM cheat sheet [xml.com].

Tedster, thanks for trying to help me. I appreciate it.

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members