xpath lets you search a document such as an xml or html file.
xpath will not show classes in the path, but will show ids with an @
symbol.
The xpath can be obtained in a few ways. One way in Chrome is to view the source of an element, right-click it and click Copy XPath
.
When I do this on the the textarea box I am answering this question in, I receive the following xpath ::
//*[@id="wmd-input"]
Do not let that confuse you though. Here is a more simplistic example
/html/body
This is the xpath of the body element.
I wrote a small function that can help you turn xpaths into elements.
function xpath(path){
for (var found, x = document.evaluate(path, document, null, XPathResult.ANY_TYPE, null), result = []; found = x.iterateNext();) {
result.push(found);
}
return result;
}
This function produces the following when running it against this textarea ::
xpath('//*[@id="wmd-input"]');
[<textarea id="wmd-input" class="wmd-input processed" name="post-text" cols="92" rows="15" tabindex="101" data-min-length></textarea>]
Now that you have the element you can modify it like this example :
var test = xpath('/html/body');
test[0].innerHTML='bye';