Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting element from DOM with JavaScript and XPath

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>
like image 934
Eric Herlitz Avatar asked Jun 24 '11 10:06

Eric Herlitz


People also ask

Can you use XPath in JavaScript?

This document describes the interface for using XPath in JavaScript internally, in extensions, and from websites. Mozilla implements a fair amount of the DOM 3 XPath, which means that XPath expressions can be run against both HTML and XML documents.

How do I find DOM element or XPath?

To find a unique attribute for your DOM: Open the context menu (right-click) for the element (such as a button, input field, div, span, or h1). Choose Inspect element (Mozilla Firefox) or Inspect (Google Chrome). Note the unique DOM attribute that you'll use to identify your element (such as id, class, or name).


2 Answers

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 = '...'; } 
like image 120
Martin Honnen Avatar answered Sep 18 '22 12:09

Martin Honnen


@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"; 
like image 40
Yann Milin Avatar answered Sep 21 '22 12:09

Yann Milin