29

I'm trying to figure out how to select the textarea in the code below using xpath and JavaScript (which is the only option here).

<body>
    <div id="calculator">
        <div id="calculatorController">
            <form action="#" method="get" onsubmit="return false">
                <p>
                    <textarea disabled="disabled"></textarea>
                </p>
            </form>
        ...

I'm trying to do something like this

var element = document.evaluate( '//body/form/p/textarea' ,document, null, XPathResult.ANY_TYPE, null );
// and write back
element.value = "Hello textarea";

But it fails

Anyone keen to help?

Thanks

Update below this

============================================================

The entire block of code looks like this. Don't forget the window.onload=function()

<html>
<head>
  <script type='text/javascript'> 
  //<![CDATA[ 
  window.onload=function(){
  var element = document.evaluate( '//body//form/p/textarea' ,document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null ).singleNodeValue;

        if (element != null) {
            element.value = 'Hello textarea';
        }

  }
  //]]> 
  </script> 
</head>
<body>
    <div id="calculator">
        <div id="calculatorController">
            <form action="#" method="get" onsubmit="return false">
                <p>
                    <textarea disabled="disabled"></textarea>
                </p>
            </form>
        </div>
    </div>
</body>
</html>
3
  • I'm no XPath expert but wouldn't the path need to be '//body/div/div/form/p/textarea'?? Commented Jun 24, 2011 at 11:04
  • 2
    I see, you put the javascript in your head section, it will execute before the DOM is loaded, try either put your code just before </body> or in the head, put your code in a function and add window.onload = function_name; Commented Jun 24, 2011 at 13:45
  • 1
    Aaaaaand, you beat me to it before I post it :) Commented Jun 24, 2011 at 13:46

2 Answers 2

38

The evaluate method does not return a DOM node as you seem to expect. You would need

var element = document.evaluate( '//body//form/p/textarea' ,document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null ).singleNodeValue;
if (element != null) {
  element.value = '...';
}
Sign up to request clarification or add additional context in comments.

6 Comments

Ooh, nice solution, proper than mine.
yes it does , maybe you're using a browser not compatible with evaluate?
Firefox 5, Chrome 12, Safari 5, all in mac os. Tried to run them both locally and from a webserver arghs
Tried his code locally on FF4 and chromium 14 here, no problems at all
Thanks, the jsfiddle helped med out :)
|
2

@Mark Robinson comment is right, your Xpath expression is wrong, you could use one of those :

//body/div/div/form/p/textarea (Mark's example)
//body//form/p/textarea (any form in body)

Plus, the evaluate function will return a XPathResult object, not the textarea, so you can't do directly element.value

Here is your example fixed:

<body>
    <div id="calculator">
        <div id="calculatorController">
            <form action="#" method="get" onsubmit="return false">
                <p>
                    <textarea disabled="disabled"></textarea>
                </p>
            </form>
        </div>
    </div>
</body>

--

var element = document.evaluate( '//body/div/div/form/p/textarea' ,document, null, XPathResult.ANY_TYPE, null );

var textarea = element.iterateNext ();
textarea.value = "Hello textarea";

2 Comments

It tend to return Error: TYPE_ERR: DOM XPath Exception 52
maybe this or this will help?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.