How to capture and alert the value of an xhtml node using JS

S

sniper

hi ;
i have this small code that consist in taking the name of the user and
writing it in the same form as an output.the name is relative to
/data/valid/string1
In my Js code i want to access the value of /data/valid/string1 (The
name seised by the user)
and alert the name as result
Can you help me to achieve this?

<?xml version="1.0" encoding="UTF-8"?>
<xhtml:html xmlns="http://xforms.websynapsis.com"
xmlns:books="http://books.websynapsis.com"
xmlns:xhtml="http://www.w3.org/1999/xhtml"
xmlns:xforms="http://www.w3.org/2002/xforms"
xmlns:ev="http://www.w3.org/2001/xml-events"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xhtml:head>
<xhtml:title>
Test case for primitive XML Schema types
</xhtml:title>

<xhtml:link rel="stylesheet" href="style.css" type="text/css"/>
<xforms:model>
<xforms:instance xmlns="">
<data>
<valid>
<string1
id="f1">Name</string1>
</valid>
</data>
</xforms:instance>
<xforms:bind nodeset="/data/valid/string1"
type="xsd:string" />
</xforms:model>
<xhtml:script id="gtre" type="text/javascript">

function initiate()
{
var p=document.getElementById('label11').firstChild.nodeValue
alert(""+p)
}

</xhtml:script>
</xhtml:head>
<xhtml:body>
<xforms:group/>
<xforms:input ref="/data/valid/string1">
<xforms:label lang="en">Name
:</xforms:label>
<xforms:action ev:event="xforms-valid">
</xforms:action>
</xforms:input>

<xhtml:input type="button" value="Afficher"
onclick="initiate();" />
<xforms:group/>

<xforms:eek:utput ref="/data/valid/string1" id="label1">
<xforms:label id="label11">Name : </xforms:label>
</xforms:eek:utput>
<xforms:group>
</xforms:group>
</xhtml:body>
</xhtml:html>

thanks for any help
 
M

Martin Honnen

sniper wrote:

i have this small code that consist in taking the name of the user and
writing it in the same form as an output.the name is relative to
/data/valid/string1
In my Js code i want to access the value of /data/valid/string1 (The
name seised by the user)
and alert the name as result

If you have XPath support as in Mozilla or as in Opera 9 then this
example should work

<xhtml:input type="button" value="test"
onclick="if (typeof document.evaluate != 'undefined') {
var element = document.evaluate('//data/valid/string1',
document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
if (element != null) {
alert(element.textContent);
}
else {
alert('No element found.');
}
}"/>

But I have no idea in which context or browser you are using that stuff,
mixing XForms (which so far no released desktop browser like IE or
Mozilla has built-in) with script might heavily depend on the browser
and/or XForms implementation/plugin you use. For instance X-Smiles has
excellent XForms support but I have doubts that it has a DOM Level 3
XPath API exposed to script.
 
S

sniper

Hi Martin Honnen,
Thanks for your reply,this code is working in my browser
but the problem is if i tape a name as an input different from "Name"
the alert message still showing me the same thing "Name"
may be ther's a need of a refresh or somthing like this
thanks;
 
M

Martin Honnen

sniper wrote:

this code is working in my browser
but the problem is if i tape a name as an input different from "Name"
the alert message still showing me the same thing "Name"
may be ther's a need of a refresh or somthing like this

Unless someone else comes along here to help out I suggest you take your
question to a newsgroup or forum dealing with that particular XForms
implementation you are using. The data you are trying to access is
XForms instance data, I don't know currently whether changes the XForms
implementation does to the instance data is supposed to show up directly
in the DOM that is exposed to script.
The XForms newsgroup might also be in a better position to tell whether
you need script at all.
 
S

sniper

hi all;
this is the solution to my problem, thanks to the help of the xforms
groupe:
the problem was that i used the static instance document from
the xforms document.
I have to use the in-memory version that XForms maintains.
We can get this version by using this:
model.getInstanceDocument(id).
The new code :

<?xml version="1.0" encoding="UTF-8"?>
<xhtml:html xmlns="http://xforms.websynapsis.com"
xmlns:books="http://books.websynapsis.com"
xmlns:xhtml="http://www.w3.org/1999/xhtml"
xmlns:xforms="http://www.w3.org/2002/xforms"
xmlns:ev="http://www.w3.org/2001/xml-events"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xhtml:head>
<xhtml:title>
Test case for primitive XML Schema types
</xhtml:title>

<xhtml:link rel="stylesheet" href="style.css" type="text/css"/>

<xforms:model id="myModel">
<xforms:instance id="myInstance" xmlns="">
<data>
<valid>
<string1 id="f1">Name</string1>
</valid>
</data>
</xforms:instance>
<xforms:bind nodeset="/data/valid/string1" type="xsd:string" />

</xforms:model>

<xhtml:script id="gtre" type="text/javascript">

function affiche()
{
var model = document.getElementById("myModel");
var instanceDoc = model.getInstanceDocument("myInstance");
if (typeof instanceDoc.evaluate != 'undefined') {
var element = instanceDoc.evaluate('//data/valid/string1',
instanceDoc, null, XPathResult.FIRST_ORDERED_NODE_TYPE,
null).singleNodeValue;
if (element != null) {
alert(element.textContent);
}
else {
alert('No element found.');
}
}
}

</xhtml:script>
</xhtml:head>
<xhtml:body>
<xforms:group/>
<xforms:input ref="/data/valid/string1">
<xforms:label lang="en">Name :</xforms:label>
<xforms:action ev:event="xforms-valid">
</xforms:action>
</xforms:input>

<xforms:group/>

<xforms:eek:utput ref="/data/valid/string1" id="label1">
<xforms:label id="label11">Name : </xforms:label>
</xforms:eek:utput>

<xforms:group/>

<xhtml:input type="button" value="test" onclick="affiche();"/>
</xhtml:body>
</xhtml:html>

That's all :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top