K
K
Kaluchi2011-11-29 10:02:21
JavaScript
Kaluchi, 2011-11-29 10:02:21

How to find a textarea element by its .value content using XPATH. '//textarea[text() = 'value']' doesn't work

Good afternoon!

I have a document whose content is filled with javascript. It contains several textarea elements. I'm trying to find them by their content using Xpath: //textarea[text() = 'value']. But this method does not work if the value in the textarea was set by the script via textarea.value = '' (or changed by manual text entry).

Can XPATH find textarea and input elements by their content in .value? If not, then why not?

Test attached:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
 <head>
  <title>Xpath test</title>
  <script type="text/javascript" language="javascript">
    function test() {
        // set new 'value' value 
       var textarea = document.getElementById('tx')
       textarea.value = 'value' 

       // XPATH search old 'initial' value
       var nodesSnapshot = document.evaluate("//textarea[text() = 'initial']", document, null, 
                                                 XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null );
       for ( var i=0 ; i < nodesSnapshot.snapshotLength; i++ ) {
         var ta = nodesSnapshot.snapshotItem(i);
         ta.value = 'fail' // set 'fail' if old value was found
       }
    }
  </script>
 </head>
 <body onload="test();">
  <textarea id="tx">initial</textarea>
 </body>
</html>

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Andrew, 2011-11-29
@Morfi

Try textContent instead of value
textarea.textContent = 'value';

X
XaBoK, 2011-11-29
@XaBoK

Now I checked through xpath.me (removing the body of the script), the request //textarea[text() = 'initial'] worked as expected. The only thing I can assume is that you did not load the required namespace.

V
Vladimir Golovanov, 2011-11-29
@Colwin

Use "//textarea[value = 'initial']", rolls in all browsers.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question