[ACCEPTED]-Selecting element from DOM with JavaScript and XPath-xpath
Accepted answer
The evaluate method does not return a DOM 1 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 = '...';
}
@Mark Robinson comment is right, your Xpath 5 expression is wrong, you could use one of 4 those :
//body/div/div/form/p/textarea (Mark's example)
//body//form/p/textarea (any form in body)
Plus, the evaluate function will 3 return a XPathResult object, not the textarea, so you 2 can't do directly element.value
Here is your 1 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";
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.