Type Here to Get Search Results !
Hi ,

We always try to serve you better. Please enter the institute name, you belong to.

Hello ,

Your daily download limit has been exhausted. Share it with your friends to download more.

How to split the text nodes from a snapshot by line breaks in javascript

 // Find all text nodes in the document

const textNodes = document.evaluate("//text()", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);

// Iterate through all text nodes

for (let i = 0; i < textNodes.snapshotLength; i++) {

    const textNode = textNodes.snapshotItem(i);

    // Split the text node by line breaks

    const lines = textNode.nodeValue.split("\n");

    // Iterate through the lines

    for (let j = 0; j < lines.length; j++) {




This code uses XPath to find all text nodes in the document and then iterates through each text node,splits the value of the text node by \n and assigns it to the variable lines. Then it iterates through all the lines and logs the value of the line. You can replace the console.log with any code you like to execute on the lines.

This is just a simple example, and in a production environment, you will want to improve it with some error handling and better DOM manipulation.

It is worth noting that this will split all the text node in the document by line breaks, if you want to change only some part of the document you need to adjust the XPath expression.

Post a Comment

* Please Don't Spam Here. All the Comments are Reviewed by Admin.