views:

45

answers:

3

What am I doing wrong here? I am trying to limit my xpath search to a specific row in my table but my query always returns the content of the span in the first row:

var query = "//span[contains(@id, 'timer')]";
var root = document.getElementById('movements').rows[1];
document.evaluate(query, root, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.textContent

Please help!

A: 

You are getting the first row:

var root = document.getElementById('movements').rows[1];

This will get the second row:

var root = document.getElementById('movements').rows[2];

You need to specify the correct index of the row you want.

Oded
A: 

Try this instead:

var query = "//td//span[contains(@id, 'timer')]";
Sarfraz
+1  A: 
"//span[contains(@id, 'timer')]"

The problem: This is an absolute XPath expression and it ignores the fact that you are evaluating it from a specific row.

Solution: To achieve the wanted result use:

.//span[contains(@id, 'timer')]

Dimitre Novatchev