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

Discussion in 'Javascript' started by sniper, Aug 25, 2006.

  1. sniper

    sniper Guest

    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
    sniper, Aug 25, 2006
    #1
    1. Advertising

  2. 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.


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Aug 25, 2006
    #2
    1. Advertising

  3. sniper

    sniper Guest

    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;
    sniper, Aug 25, 2006
    #3
  4. 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.


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Aug 25, 2006
    #4
  5. sniper

    sniper Guest

    ok thank you Martin for your help
    sniper, Aug 25, 2006
    #5
  6. sniper

    sniper Guest

    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 :)
    sniper, Sep 7, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    1,445
  2. Eddy C
    Replies:
    6
    Views:
    6,416
    Eddy C
    Oct 18, 2005
  3. Tjerk Wolterink
    Replies:
    2
    Views:
    1,420
    Dimitre Novatchev
    Aug 24, 2006
  4. Mersh
    Replies:
    0
    Views:
    486
    Mersh
    Mar 13, 2007
  5. njsimha
    Replies:
    0
    Views:
    777
    njsimha
    Sep 16, 2008
Loading...

Share This Page