Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

document.evaluate() on a string or virtual document

Status
Not open for further replies.

spiderplant0

Programmer
Oct 4, 2010
6
GB
Hi,
How do I use the document.evaluate() method on a string of HTML to find something within the string?

E.g. if I have this string:
var stg = "<div>The best-laid schemes o' <span>mice</span> an' men</div>";
I'd like to convert this string into something that I can run the document.evaluate() on so that I can find the contents of the SPAN element without loading the string into a real browser page.

(I know how to use document.evaluate() as I've used it before or real web pages - I just dont understand how to run it on a 'virtual document' (this probably isnt the correct term)).

Thanks
 
here is the solution in case anyone's interested
Code:
 var stg = "<div>The best-laid<br> schemes o' <br><span>mice</span> an' men</div>";
    var newDiv = document.createElement("div");
    newDiv.innerHTML = stg;

    var recordNodes = document.evaluate(
        ".//span",
        newDiv,
        null,
        XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
        null );

     console.log(recordNodes.snapshotLength);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top