como obtener los nodos html de un lista de nodos in javascript

To obtain the HTML nodes of a list of nodes in JavaScript, you can use a loop to iterate over each node in the list and use the innerHTML property to obtain its HTML content. Here is an example code snippet:

index.tsx
const nodeList = document.querySelectorAll('.my-class'); // get the list of nodes with class "my-class"
const htmlNodes = []; // initialize an empty array to store the HTML nodes
for (let i = 0; i < nodeList.length; i++) { // iterate over each node in the list
  const html = nodeList[i].innerHTML; // obtain the HTML content of the node
  htmlNodes.push(html); // add the HTML content to the array of HTML nodes
}
console.log(htmlNodes); // show the list of HTML nodes in the console
485 chars
8 lines

In this example, we first obtain a list of nodes with the class "my-class" using the querySelectorAll method. We then create an empty array to store the HTML nodes, and loop over each node in the list. For each node, we use the innerHTML property to obtain its HTML content, and add it to the array of HTML nodes using the push method. Finally, we display the list of HTML nodes in the console using the console.log method.

related categories

gistlibby LogSnag